Why we are using equals() method in HashMap and HashSet without implementing the comparator interface?. I have seen some example programs in the above concept. But there without comparator interface, they are using equals() and hashcode() method. My question is ,can we use those methods without comparator interface? And also can we use equals() and hashcode() and compareTo() methods with comparable interface ?
Asked
Active
Viewed 174 times
-4
-
6`equals` and `hashCode` are about seeing whether objects represent the same value. The `Comparator` interface is about telling you what order two things should go in. They are not the same thing. – khelwood Feb 21 '20 at 15:16
-
2Please see [compareTo() vs equals()](https://stackoverflow.com/questions/1551235/compareto-vs-equals) – Hovercraft Full Of Eels Feb 21 '20 at 15:16
-
2Let me ask counterquestion: why do you think we need Comparator for HashMap or HashSet? For what purpose we would need to implement it? – Pshemo Feb 21 '20 at 15:29
1 Answers
5
HashMap
and HashSet
care about comparing keys or elements for equality - they don't care about ordering, unlike (say TreeMap
and TreeSet
). That's why they use equals()
and hashCode()
.
Comparators are all about ordering - and it's entirely possible to have a class where you have no specific order, but do have the notion of equality. For example, a 2D Point
type has no particularly natural ordering (you could do Y-then-X or X-then-Y if you wanted to define comparators for it) but does have a natural equality operation. That means it's fine to be the key in a HashMap
or an element in a HashSet
.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
1Well actually, `HashMap` can fallback to using `Comparable` in some circumstances (OpenJDK implementation). – Tom Hawtin - tackline Feb 21 '20 at 15:20
-
1@TomHawtin-tackline It does not fallback to it, it potentially uses Comparable as part of an optimization when ordering a red-black tree, which only occurs if the bucket is quite large – Michael Feb 21 '20 at 15:21
-
@Michael I'm going to call that falling back. ***fallback** an alternative plan that may be used in an emergency*. – Tom Hawtin - tackline Feb 21 '20 at 15:48
-
@TomHawtin-tackline: I guess it depends whether it's used as an implementation detail or whether it changes visible behavior. For example, if two values compare as equal under `x.equals(y)` but `x.compareTo(y)` returns non-zero, will `HashMap` treat them as different keys? (Or the other way round, where `equals` returns false but `compareTo` returns 0.) – Jon Skeet Feb 21 '20 at 15:50
-
@JonSkeet Yeah, let me get this the right way around... If you have `equals` says the same but `compareTo` says different you have a, possibly intermittent, problem. Anyway, it works out that `BigDecimal` (1.0 not `equals` 1.00) fails correctly. – Tom Hawtin - tackline Feb 21 '20 at 15:56
-
@TomHawtin-tackline: Fails as in "HashMap always obeys equals"? That's what I'd hope for. (Deleted the bit about Double, which I was wrong about.) – Jon Skeet Feb 21 '20 at 16:03
-
@JonSkeet It fails the "Strong recommendation" of `Comparable`. `compareTo` and `equals` been inconsistent the other way around, IIRC, you could end up with multiple same keys in a single `HashMap`, in which case the map could not possibly obey `equals`. – Tom Hawtin - tackline Feb 21 '20 at 16:09
-
@TomHawtin-tackline No, that's wrong. HashMap always obeys equals. `compareTo` is only used for inserting to a red-black tree on insertion, and for traversing the tree on look-up. `equals` is always the final check, the ultimate test of equality. All that matters is that `compareTo` is consistent with itself. It does not matter if Comparable is inconsistent with `equals`. Think about it -- HashMap could not sensibly make an optimization based upon an aspect of an interface that the interface itself does not even make part of its contract (while it strongly recommends it, it is not mandatory). – Michael Feb 21 '20 at 16:15
-
@Michael I went to the effort of writing an example, but then searched and found a (less subtle) example. https://stackoverflow.com/q/34305067/4725 If `HashMap` had to check `equals` it wouldn't be any faster. – Tom Hawtin - tackline Feb 21 '20 at 22:59