Java HashSet with a custom equality criteria?
Nope, you've found exactly the solution you're supposed to use.
Even for TreeSet
, it's frowned upon to use comparison criteria that aren't compatible with equals
:
Note that the ordering maintained by a sorted set (whether or not an explicit comparator is provided) must be consistent with equals if the sorted set is to correctly implement the Set interface.
I don't know about Apache Commons, but Guava specifically rejected requests for this sort of thing, although you can achieve what you want using Guava Equivalence:
Equivalence<T> equivalence = new Equivalence<T>() {
@Override
protected boolean doEquivalent(T a, T b) {
return CustomComparator.equals(a, b);
}
@Override
protected int doHash(T item) {
return CustomHashCodeGenerator.hashCode(item);
}
};
List<T> items = getItems();
Set<Equivalence.Wrapper<T>> setWithWrappedObjects = items.stream()
.map(item -> equivalence.wrap(item))
.collect(Collectors.toSet());
There are a couple of third-party collections frameworks that allows custom equality logic. This is perfect for overriding equality for objects that you can't alter the source.
- Trove
Trove's maps/sets support the use of custom hashing strategies, allowing you to tune collections based on characteristics of the input data. This feature also allows you to define hash functions when it is not feasible to override Object.hashCode().
- HE-Collection
To achive this, any type that needs a standard correction, must implement the HE-Collection interface EqualsAndHashCorrection. This interface defines the methods hashCodeInHeCollection() and equalsInHeCollection(Object), that serve as correction for the incorrect implemented methods hashCode() and equals(Object).
You are correct, when you want to use any of the Trees
(TreeMap
, TreeSet
) the objects you add must implement Comparable
.
For primitive types, Java has solved this for you.
For custom objects you have 3 possibilities:
One of your object already has an unique id of a primitive type or an Type that already implements
compareTo()
(likeString
) Then use this field for compareTo, if the values of the others are not important for equality. (But thenequals()
must also only use this one field)Use
EqualsBuilder
from Apache: This works with reflection, and is not the fastest solutionWrite it your own, read some tutorial how to do that: e.g:
Josh Bloch: Effective java 2nd Edition
But don't forget that equals()
, and compareTo()
must be compatible (and hashCode()
, too), such that you do not violate the equals contract. (The contract itself is less understandable, but it gets clear if you foillow one of that equals tutorials.)
Or forget that all, and use a HashSet
, HashMap
.