I\'m trying to find separate the duplicates and non-duplicates in a List
by adding them to a Set
and List
while using Stream.fil
Your code does not work, because the stream is not consumed. You have provided only the intermediate operations, but until you call a terminating operation like forEach
, reduce
or collect
, nothing you have defined in your stream will be invoked.
You should rather use peek
to print the elements going through the stream and collect
to get all the elements in the list:
List extras = strings
.stream()
.filter(x -> !distinct.add(x))
.peek(System.out::println)
.collect(Collectors.toList());
Using forEach
to fill the empty collection created before is a code smell and has nothing to do with functional programming.