Answer the question
In order to leave comments, you need to log in
How to remove all unique elements from a collection?
How can I gracefully remove all unique data from a list?
For example, from this:
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("A");
list.add("B");
list.add("X");
Answer the question
In order to leave comments, you need to log in
list = list.stream()
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question