List integers = Arrays.asList(1, 2, 3, 5, 6, 8, 9, 10);
integers.stream().filter((integer) -> integer % 2 == 0).collect(Collectors.toList());
You can use the Stream.reduce(U identity, BiFunction accumulator, BinaryOperator combiner) method, which takes three parameters:
For example:
BinaryOperator> combiner = (x, y) -> { x.addAll(y); return x; };
BiFunction, Integer, ArrayList> accumulator = (x, y) -> {
if (y % 2 == 0) {
x.add(y);
}
return x;
};
List list = Stream.of(1, 2, 3, 5, 6, 8, 9, 10).reduce(new ArrayList(),
accumulator,
combiner);
System.out.println(list);
Note that this solution may not work for parallel Streams. Also, it's way too easier to stick to the .filter()
approach, so I strongly advice you to do so.