How to link Java HashMap? How do I access all collision values?
I read somewhere that HashMap uses links to resolve conflicts But if so How to access all elements with the same key value
For example:
HashMap<Integer,String> hmap = new HashMap<Integer,String>(); hmap.put(1,"1st value"); hmap.put(1,"2nd value"); hmap.put(1,"3rd value"); hmap.put(1,"4th value");
Now, if I do hmap Get (1) it returns "fourth value"
If it does, it does look like a link
How can I get other values?
hmap. Get (1) returns only the first value
My second question is,
If it makes linear links How to delete any value of a key Suppose I want to delete "4th value" from my HashMap and want to keep all other values of the same key, what should I do?
If I do
, it removes the entire chain
Solution
HashMap cannot store multiple values for the same key
Chaining is used to resolve hash conflicts, that is, when different keys have the same hash Therefore, it is not about storing multiple values with the same key, but about multiple values whose keys have the same hash value
A data structure that can store multiple values for the same key is called a multi graph Unfortunately, there is no built - in implementation of Multimap in JRE
If you need multiple maps, you can maintain a list map (recommended by matsev) or use an existing multi map implementation in a third-party library, such as Google guava
You can also see:
> Collision resolution