Is HashMap internally implemented in Java using LinkedList or Array?

前端 未结 4 1561
面向向阳花
面向向阳花 2020-12-25 15:40

How is HashMap internally implemented? I read somewhere that it uses LinkedList while other places it mentions Arrays.

I tried studying the

4条回答
  •  無奈伤痛
    2020-12-25 16:03

    HashMap internally uses Entry for storing key-value pair. Entry is of LinkedList type.

    Entry contains following ->

    K key,

    V value and

    Entry next > i.e. next entry on that location of bucket.

    static class Entry {
         K key;
         V value;
         Entry next;
    
         public Entry(K key, V value, Entry next){
             this.key = key;
             this.value = value;
             this.next = next;
         }
    }
    

    HashMap diagram -

    custom Implementation of HashMap

    From : http://www.javamadesoeasy.com/2015/02/hashmap-custom-implementation.html

提交回复
热议问题