17

I have a class which I want to set up as keys in HashMap. I already have implemented the compareTo method for that class. But still when I do:

map.put(new MyKey(dummyArguements) , dummyValue );
System.out.println(map.get( new MyKey(dummyArguements) ) );

I get null. So that means hashmap is not able to identify that the two keys (for get & put call) are same.

Could someone help me here please ?

Nikhil Garg
  • 3,944
  • 9
  • 30
  • 37

7 Answers7

40

You need to implement hashCode() and equals(). compareTo() is additionally required for sorted map/set.

See this question for details.

Community
  • 1
  • 1
Mike Q
  • 22,839
  • 20
  • 87
  • 129
  • 1
    I mean no disrespect (nor do I not believe this to be true), but **how** do you know this? I've been reading the [`HashMap` documentation](http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) over-and-over; following interfaces and inheritances.... and there is nothing that remotely implies the methods mentioned above need to be implemented nor overridden (since they're already inherited from the `Object` root class) for it to work properly with `HashMap`. –  Apr 17 '15 at 14:38
  • The definition of some methods on the Map interface establishes some of the rules. For example containsKey() specifically mentions equals(). The hashCode() documentation on Object specifies the relationship between it and equals(). I do however agree with your point - HashMap documentation should reference the hashCode() method and document it's use/requirement for keys of HashMap. – Mike Q Nov 05 '15 at 13:26
  • Also you class should be `immutable` – Sadegh Oct 15 '17 at 13:18
13

You should implement equals() and hashCode(). Your class should also be immutable. If it is mutable, it's hash code can change after adding it to map. Then the map can have problems finding it.

amorfis
  • 15,390
  • 15
  • 77
  • 125
3

1) In general for collections, what you want to override is the equals() method (and also the hashcode() method) for your class. compareTo()/Comparable and Comparator are typically used for sorting and only take the place of using the equals() method for object equivalance in some cases - examples are implementers of SortedSet such as TreeSet.

2) Please conform to Java naming standards in your code. Your class names should be capitalized... e.g new MyKey(dummyArguments). See http://www.oracle.com/technetwork/java/codeconventions-135099.html#367 (and http://www.oracle.com/technetwork/java/codeconvtoc-136057.html) for more detail.

whaley
  • 16,075
  • 10
  • 57
  • 68
0

Do you have the hashCode() defined? compareTo is needed for sorting.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
0

HashMap doesn't check compareTo();

HashMap checks hashCode() and equals().

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
0

When using Collections that rely on hashing like Map and Set you have to implement the equals() and hashCode() to guarantee correct functionality. If you don't a new myKey will always be different from the key stored in the map because it uses the default implementations of equals() and hashCode().

kostja
  • 60,521
  • 48
  • 179
  • 224
0

As of java8 you should also implement Comparable (adding compareTo) because if the number of hash clashes exceeds 11, HashMap stores the entries in a binary tree. If you don't, performance suffers