Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. MyKey key = new MyKey("shreyansh"); //assume hashCode=1234
  2. myHashMap.put(key, "value");
  3.  
  4. // Below code will change the key hashCode() and equals()
  5. // but it's location is not changed.
  6. key.setName("jogi"); //assume new hashCode=7890
  7.  
  8. //below will return null, because HashMap will try to look for key
  9. //in the same index as it was stored but since key is mutated,
  10. //there will be no match and it will return null.
  11. myHashMap.get(new MyKey("shreyansh"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement