Finding duplicate entries in Collection
collections, duplicates, equality, java
Solution
I've created a new interface akin to the `IEqualityComparer<T>` interface in .NET.
Such a `EqualityComparator<T>` I then pass to the following method which detects duplicates.
public static <T> boolean hasDuplicates(Collection<T> collection,
EqualsComparator<T> equalsComparator) {
List<T> list = new ArrayList<>(collection);
for (int i = 0; i < list.size(); i++) {
T object1 = list.get(i);
for (int j = (i + 1); j < list.size(); j++) {
T object2 = list.get(j);
if (object1 == object2
|| equalsComparator.equals(object1, object2)) {
return true;
}
}
}
return false;
}
This way I can customise the comparison to my needs.
Problem
Is there a tool or library to find duplicate entries in a Collection according to specific criteria that can be implemented? To make myself clear: I want to compare the entries to each other according to specific criteria. So I think a `Predicate` returning just `true` or `false` isn't enough. I can't use `equals`.