Issue
I am developing a parser that needs to put key value pairs in hashmap. A key can have multiple values which I can do in this way HashMap<String,ArrayList<String>>
.
What happens if the number of keys are very large and they start matching with other key's hashcode? Will that rewrite previous key's value?
Solution
If the hash of the key in the map collides with an existing key, the Map will re-arrange or keep the keys in a list under that hash. No keys will get overwritten by other keys that happen so be sorted in the same bucket.
If multiple threads are using the map concurrently, you might want to synchronize access to the map if it does not handle concurrent access. (Some standard Maps do, other don't. The Java Collections package does contain wrapper classes that add synchronisation.)
Answered By - rsp
Answer Checked By - Cary Denson (JavaFixing Admin)