I am stuck around while studying hashCode() in java. I wrote a sample code PFB snippet
public class Shared {
String type;
Date date;
public Shared(String type) {
this.type = type;
}
public void setType(String type) {
this.type = type;
this.date = new Date(19894894823948L);
}
public static void main(String arg[]) {
HashMap<Shared, String> hm = new HashMap<Shared, String>();
Shared key = new Shared("me");
Shared key1 = new Shared("me1");
hm.put(key1, "value1");
hm.put(key, "value");
System.out.println(key.hashCode());//returning hashcode suppose X
key.type=null;
key.setType("type");
System.out.println(key.hashCode());// it's even returning X again
key.type="null";
System.out.println(key.hashCode());// it's even returning X again.
}
I am really confused how this is possible even after changing the value contained within the key hashCode is same. it's literally breaking the immutable key concept.
Is the hashCode() method architecture/platform dependent?