I want to group a collection but I don't want the aggregations to include some values. What's the best solution to do that?
A solution could be a collector filtering(Predicate,Collector)
but there is no such collector. Is there any way to do it without implementing your own collector?
IntStream.range(0,100).collect(
groupingBy(
i -> i % 3,
HashMap::new,
filtering( i -> i % 2 == 0, toSet() )
)
);
Map<Integer, Set<Integer>>
. I want the Sets to contain only elements that satisfy predicatei -> i % 2 == 0
. I can't filter the collection before grouping because then the result will not contain some groups(in general case). – Paroicous