Why is LinkedList being used as an implementation for a HashMap and not another Hashmap?

Does anyone know why buckets for HashMaps were chosen to be implemented via a LinkedList and not another Hashmap. It seems like contains or get would be O (1) rather than amortized O (1) if the buckets became HashMaps themselves.

+3


source to share


2 answers


Imagine buckets are implemented using HashMap and then a lot of collisions. A good hash function should try to eliminate collisions, but we are thinking about a very large number of items. Collisions will now be saved in the bucket's HashMap implementation. This HashMap should have space to store its own buckets. What if there were so many elements that this HashMap had multiple collisions inside it? Even with a very good hash function, down the line somewhere, HashMap buckets (say B, for example) will start to interfere with HashMap A buckets that have multiple buckets, one of which is implemented by B.

LinkedList won't have this problem. Also keep in mind that when the bucket is assigned to the HashMap, it is the memory that is locked outside - even if it is empty. The LinkedList will grow dynamically and will not require more memory than the number of entries.



To summarize, you can of course implement whatever you want. You can use ArrayList or just an array. But they also have their own problems (deletion leads to a time complexity of resizing O (n), and arrays must be of a fixed size). Therefore, considering all the tradeoffs, LinkedList turned out to be the best choice.

+1


source


HashMaps will still amortize O (1) if they were implemented as buckets in another HashMap. Let's say that two strings are hashed at the same index in the internal data structure of the HashMap. If that index contained another HashMap, the two strings would again be hashed at the same index, but into the HashMap bucket. Then you will have to decide how to handle conflicts in the HashMap bucket. So implementing HashMap with HashMaps as buckets will create two collisions instead of one. Also, HashMaps uses much more memory for a given dataset than LinkedLists. They require more memory slots than data records to guarantee O (1) amortization.



Yes, LinkedLists are not ideal for buckets because it takes O (n) time for the bucket size to get the correct values. LinkedList buckets shouldn't get very big because the HashMap should be big enough to keep collisions to a minimum.

+1


source







All Articles