More specifically I want an interface to compare objects which can be only compared for equality [e.g. complex numbers] but don’t have total order on them.
It should have [Note that it only returns a boolean yes/no]
boolean Equals(T object1, T object2);
and a hashcode function. So that when I distribute them into buckets using objects say to collect “equal” objects, 2 “equal” objects don’t end up in two different buckets.
int getHashCode(T object);
Does Java have one? I searched and couldn’t find it.
I am trying to use this in Hadoop Map Reduce to distribute “equal” objects to same reduce job so that I can operate on all “equal” objects. I only care about whether objects are equal or not and don’t need total order. But if two objects are equal they should have same hash code. Otherwise they will end up in two different reduce jobs.
Please note that I know about equals and hashcode of object. But I want an external comparator which say depends on only part of an object. So object’s notion of equality differs from mine.
There’s no built-in type that is used for this in Java. It’s a “hole” in the collections design, IMO. There’s the string-specific Collator class which is about as close as it gets, I’m afraid.
There’s no way of customizing the built-in maps to use a specific kind of equality comparison, worse luck. It’s entirely reasonable to want this functionality, and a real pain that it’s not already present.
You could create your own such interface of course, and write your own map variants which use it… but having to do so sucks 🙁