Is there some implementation of java.util.Map that does not uses HashCode?
I have the following problem:
- I store an object associated to another object on a HashMap;
- Change a property from the key object used on step 1;
- As the hashcode is used to store the keys on the regular implementation of HashMap, when I perform a
get()on theHashMap, I get null, because the old object hashCode was different at step 1.
Is there a solution for that? Or should I really use just immutable fields for my equals / hashCode methods?
IdentityHashMap uses the Object identity instead of the hashCode; however that does mean that you require the original object used as key to retrieve the value of the map. Other options would be redefine the hashcode to exclude the mutable parts of the object, or – if you can’t redefine the hashCode for some reason – wrap the object in another object which provides a stable hashCode.