get duplicated from list java code example
Example 1: java 8 get duplicates in list
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 4, 4);
Set<Integer> nbrRemovedSet = new HashSet<>();
Set<Integer> nbrSet = numbers
.stream()
.filter(n -> !nbrRemovedSet.add(n))
.collect(Collectors.toSet());
Set<Integer> nbrSet = numbers
.stream()
.filter(i -> Collections.frequency(numbers, i) >1)
collect(Collectors.toSet());
Example 2: java find duplicates in array
for (String name : names)
{
if (set.add(name) == false)
{
}
}