Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HashMap with custom key

Tags:

java

map

Quick Question: If I want to use HashMap with a custom class as the key, must I override the hashCode function? How will it work if I do not override that function?

like image 729
MBZ Avatar asked Jul 01 '12 10:07

MBZ


People also ask

How HashMap pass custom object as key?

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 use custom object as key in TreeMap?

As TreeMap sorting is based only on the keys, I'm using a custom object as key in a treemap. I have respected in my opinion the contract between equals and compareTo in this case, if the two objects are equal, te compareTo returns 0.

Can we use user defined object as key in HashMap in Java?

Can we use user defined class object as a key in Hashmap? yes sir..... you can use any custom class object in hashmap for key purpose. But firstly you have to make that class either Comparable or Comparator.

What happens if we put a key object in a HashMap which exists?

What happens if we put a key object in a HashMap which exists? Explanation: HashMap always contains unique keys. If same key is inserted again, the new object replaces the previous object.


2 Answers

If you don't override hashCode AND equals you will get the default behaviour which is that each object is different, regardless of its contents.

like image 82
Peter Lawrey Avatar answered Oct 17 '22 07:10

Peter Lawrey


Technically, you don't have to override the hashCode method as long as equal objects have the same hashCode.

So, if you use the default behaviour defined by Object, where equals only returns true only for the same instance, then you don't have to override the hashCode method.

But if you don't override the equals and the hashCode methods, it means you have to make sure you're always using the same key instance.

E.g.:

MyKey key1_1 = new MyKey("key1");

myMap.put(key1_1,someValue); // OK

someValue = myMap.get(key1_1); // returns the correct value, since the same key instance has been used;

MyKey key1_2  = new MaKey("key1"); // different key instance

someValue = myMap.get(key1_2); // returns null, because key1_2 has a different hashCode than key1_1 and key1_1.equals(key1_2) == false

In practice you often have only one instance of the key, so technically you don't have to override the equals and hashCode methods.

But it's best practice to override the equals and hashCode methods for classes used as keys anyway, because sometime later you or another developer might forget that the same instance has to be used, which can lead to hard to track issues.

And note: even if you override the equals and hashCode methods, you must make sure you don't change the key object in a way that would change the result of the equals or the hashCode methods, otherwise the map won't find your value anymore. That's why it's recommended to use immutable objects as keys if possible.

like image 27
Puce Avatar answered Oct 17 '22 08:10

Puce