Java – does the implementation of hasmap store key value pairs in the linked list

I read in a book that when we place an element in a HashMap, it is stored internally in a bucket My question is

>Does HashMap store key value pairs as a linked list? Or is it stored in the linked list only in case of collision? > How does it retrieve objects when two different objects are stored in the same bucket?

thank you!

Solution

Many details are in http://en.wikipedia.org/wiki/Hash_table

See also internal implementation of Java util. HashMap and HashSet

Of course, you can use source, Luke

Update: to specifically answer your Q, it stores an entry that references the next item (if any) in the bucket If there is only one item in the bucket, the reference will be null:

static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>