Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting own class as key in java Hashmap

Tags:

java

hashmap

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 ?

like image 343
Nikhil Garg Avatar asked Sep 08 '10 07:09

Nikhil Garg


People also ask

Can we have custom class as key in HashMap?

Custom Key Classes We can conclude that to use a custom class for a key, it is necessary that hashCode() and equals() are implemented correctly. To put it simply, we have to ensure that the hashCode() method returns: the same value for the object as long as the state doesn't change (Internal Consistency)

How do you make a class object a key in HashMap?

If you want to make a mutable object as a key in the hashmap, then you have to make sure that the state change for the key object does not change the hashcode of the object. This can be done by overriding the hashCode() method. But, you must make sure you are honoring the contract with equals() also.

Can we create our own HashMap class in Java?

If we wish to create a HashMap of our own class, we need to ensure that the hashcode() of the key of HashMap doesn't change as if it happens then it is impossible to get object value of the key from HashMap. On runtime, JVM processes hash code for each object and give it on interest.

What should you do when using a custom Java object as a key in a map?

Therefore, to use an object as a key in HashMap , HashSet , or Hashtable in Java, we need to override equals and hashcode methods of that object since default implementation of these methods simply check for the instance equality.


3 Answers

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

See this question for details.

like image 197
Mike Q Avatar answered Sep 26 '22 02:09

Mike Q


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.

like image 21
amorfis Avatar answered Sep 24 '22 02:09

amorfis


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.

like image 30
whaley Avatar answered Sep 25 '22 02:09

whaley