Search in sources :

Example 11 with IntStream

use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.

the class CustomTest method testCustomIntermediateOperator_Zip.

@Test
public void testCustomIntermediateOperator_Zip() {
    final IntBinaryOperator op = new IntBinaryOperator() {

        @Override
        public int applyAsInt(int left, int right) {
            return left + right;
        }
    };
    IntStream s1 = IntStream.of(1, 3, 5, 7, 9);
    IntStream s2 = IntStream.of(2, 4, 6, 8);
    int[] expected = { 3, 7, 11, 15 };
    IntStream result = s1.custom(new CustomOperators.Zip(s2, op));
    assertThat(result.toArray(), is(expected));
}
Also used : IntBinaryOperator(com.annimon.stream.function.IntBinaryOperator) IntStream(com.annimon.stream.IntStream) CustomOperators(com.annimon.stream.CustomOperators) Test(org.junit.Test)

Example 12 with IntStream

use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.

the class DistinctTest method testDistinctLazy.

@Test
public void testDistinctLazy() {
    Integer[] expected = { 1, 2, 3, 5, -1 };
    List<Integer> input = new ArrayList<Integer>();
    input.addAll(Arrays.asList(1, 1, 2, 3, 5));
    IntStream stream = Stream.of(input).mapToInt(Functions.toInt()).distinct();
    input.addAll(Arrays.asList(3, 2, 1, 1, -1));
    List<Integer> actual = stream.boxed().toList();
    assertThat(actual, contains(expected));
}
Also used : ArrayList(java.util.ArrayList) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 13 with IntStream

use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.

the class IntStreamMatcherTest method testElements.

@Test
public void testElements() {
    final IntStream stream = IntStream.of(-813, 123456, Short.MAX_VALUE);
    final Integer[] expected = new Integer[] { -813, 123456, (int) Short.MAX_VALUE };
    final Matcher<IntStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(IntStream.empty()));
    assertThat(matcher, description(allOf(containsString("IntStream elements"), containsString(Stream.of(expected).map(new Function<Integer, String>() {

        @Override
        public String apply(Integer t) {
            return String.format("<%s>", t.toString());
        }
    }).collect(Collectors.joining(", "))))));
}
Also used : Function(com.annimon.stream.function.Function) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Aggregations

IntStream (com.annimon.stream.IntStream)13 Test (org.junit.Test)11 CustomOperators (com.annimon.stream.CustomOperators)2 ArrayList (java.util.ArrayList)2 DoubleStream (com.annimon.stream.DoubleStream)1 DoubleBinaryOperator (com.annimon.stream.function.DoubleBinaryOperator)1 Function (com.annimon.stream.function.Function)1 IntBinaryOperator (com.annimon.stream.function.IntBinaryOperator)1 IntPredicate (com.annimon.stream.function.IntPredicate)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1