I'm sorry if this question was already asked, but i don't fine an answer to my question. I'm working on HashMap i put two values (7,"value test 1") (7,"value test 2) According to the specification java API HashMap put the first values is replaced by the second .
My question is when comes the resolution of collision ? why my second value is not store in linkedList or store in another place in the hashMap ? Is it due to the equals or hascode method ??
Best Regards
This has nothing to do with hash collision. Hash collisions (ie., keys with the same hashcode()
) are handled correctly by the HashMap. In your example, both keys are equal (ie., 7.equals(7) == true
), so the old value is replaced.
In the following example
Map<Integer, String> map = new HashMap<>();
map.put(7, "value 1");
map.put(7, "value 2");
System.out.println(map.get(7));
what would you expect in the last line to happen?
Maybe you are looking for a multimap?