I have a requirement to log/sysout
the filtered values in Java Streams. I am able to log/sysout
the non-filtered value using peek()
method
You could use
Map> map = persons.stream()
.collect(Collectors.partitioningBy(p -> "John".equals(p.getName())));
System.out.println("filtered: " + map.get(true));
List result = map.get(false);
or, if you prefer a single-statement form:
List result = persons.stream()
.collect(Collectors.collectingAndThen(
Collectors.partitioningBy(p -> "John".equals(p.getName())),
map -> {
System.out.println("filtered: " + map.get(true));
return map.get(false);
}));