Doing this in a single Stream pipeline would probably require the wanted operation to act like a full barrier: in order to group the values, we need to process them all to see which values are even and odd (and be able to sum them).
As such, it is probably preferable to use a temporary data structure to hold the count of even and odd values, which in this case would be a Map<Integer, Integer>
or even a Map<Boolean, Integer>
(where, for example, the key would be true
for even values and false
for odd values, using Collectors.partitioningBy
).
Note that you don't need to use the StreamEx library here. You can do this directly the Stream API:
public static void main(String[] args) {
Map<Integer, Integer> map =
Stream.of(0, 1, 2, 4)
.collect(Collectors.groupingBy(
i -> i % 2,
Collectors.summingInt(i -> i)
));
Stream<Integer> stream = map.values().stream();
}