I am confused.
According to many websites, including the ones below, a HashMap must have unique non-repeating keys, but the values can be repeated.
https://data-flair.training/blogs/hashset-vs-hashmap-in-java/#:~:text=A HashMap class is also,can be added to it.
https://stackoverflow.com/questions/27128247/hashmap-duplicate-values-identify-the-duplicates (Top answer)
Why then, is there a collision if two different keys have the same value, as described in this website?
https://www.baeldung.com/java-hashmap-advanced
Take the code shown below.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
HashMap<Integer, String> test = new HashMap<Integer, String>();
test.put(1, "Joe");
test.put(2, "Tim");
test.put(3, "Harry");
test.put(4, "Tim");
System.out.println(test);
}
}
I am not getting any errors despite both the keys of 2 and 4 having the same value, "Tim". I don't understand when a collision could occur. It all seems contradictory. Its legal to have the same value for different keys but apparently that can also cause a collision?
Can anyone provide an example of a HashMap collision and explain how that is different from the example I shared in the code snippet above?
Thank you.
I understand that HashMap doesn't allow insertion of duplicate values and it replaces the last duplicate value with the latest entry.
Is there a way to print the duplicates which were found during ...