I need to find if they were any duplicates in fullName - occupation pair, which has to be unique
Based on this comment it seems that you don't really care about which Person
objects were duplicated, just that there were any.
In that case you can use a stateful anyMatch
:
Collection<Person> input = new ArrayList<>();
Set<List<String>> seen = new HashSet<>();
boolean hasDupes = input.stream()
.anyMatch(p -> !seen.add(List.of(p.fullName, p.occupation)));
You can use a List
as a 'key' for a set which contains the fullName
+ occupation
combinations that you've already seen. If this combination is seen again you immediately return true
, otherwise you finish iterating the elements and return false
.