[Solved] HashMap get method throwing error on hm.get(hm) [closed]


Trying to put a HashMap as a key inside itself is a bad idea.

After hm.put(hm,hm), your HashMap contains a key whose hashCode() is hm.hashCode(). hm.hashCode() is a function of the hashCode() of all the Map‘s entries. The hashCode() of an entry is a function of the hashCode() of the key and value (both are hm in your case). Hence, in order to compute hm.hashCode(), you have to compute hm.hashCode(). This results in infinite recursion.

Calling hm.get(hm); required computing hm.hashCode(), leading to infinite recursion and StackOverflowError.

solved HashMap get method throwing error on hm.get(hm) [closed]