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
In order to the filter to be applied, you need to call a terminal operation like collect(). In that case you can assign the items which pass the filter directly to the extras list instead of use map function.
Try something like this:
List strings = Arrays.asList("foo", "bar", "foo", "baz", "foo", "bar");
Set distinct = new HashSet<>();
List extras = strings
.stream()
.filter(x -> !distinct.add(x))
.collect(Collectors.toList());