I have a users list and I want to find all users having duplicate names:
var allNames = users
.stream()
.map(u -> u.getName()).collect(Collectors.toList());
var duplicateNames = allNames
.stream()
.filter(i -> Collections.frequency(allNames, i) > 1)
.collect(Collectors.toSet());
Can I improve/simplify the above solution?
For example, actually I create a list with all names and then filter it. How can I traverse the list to find its duplicate names without creating the additional list allNames?