JAVA HASHMAP的原理分析

來源:互聯網
上載者:User

還是來整體看一下HashMap的結構吧. 如下圖所示(圖沒畫好),方框代表Hash桶,橢圖代表 桶內的元素,在這裡就是Key-value對所組成Map.Entry對像.

如果有多個元索被Hash函數定位到同一個桶內,我們稱之為hash衝突,桶內的元素組成單向 鏈表.讓我們看一下hashMap JDK源碼(因篇幅關係,刪除了部分代碼與注釋,感興可以查看 JDK1.6源碼):

public class HashMap<K,V>
     extends AbstractMap<K,V>
     implements Map<K,V>, Cloneable, Serializable
{
     static final int DEFAULT_INITIAL_CAPACITY = 16;
     static final int MAXIMUM_CAPACITY = 1 << 30;
     static final float DEFAULT_LOAD_FACTOR = 0.75f;
     transient Entry[] table;
     transient int size;
     int threshold;
     final float loadFactor;
     transient volatile int modCount;
     public HashMap(int initialCapacity, float loadFactor) {
         if (initialCapacity < 0)
             throw new IllegalArgumentException("Illegal initial  capacity: " +
                                                 initialCapacity);
         if (initialCapacity > MAXIMUM_CAPACITY)
             initialCapacity = MAXIMUM_CAPACITY;
         if (loadFactor <= 0 || Float.isNaN(loadFactor))
             throw new IllegalArgumentException("Illegal load  factor: " +
                                                 loadFactor);
         // Find a power of 2 >= initialCapacity
         int capacity = 1;
         while (capacity < initialCapacity)
             capacity <<= 1;
         this.loadFactor = loadFactor;
         threshold = (int)(capacity * loadFactor);
         table = new Entry[capacity];
         init();
     }

     public V get(Object key) {
         if (key == null)
             return getForNullKey();
         int hash = hash(key.hashCode());
         for (Entry<K,V> e = table[indexFor(hash,  table.length)];
              e != null;
              e = e.next) {
             Object k;
             if (e.hash == hash && ((k = e.key) ==  key || key.equals(k)))
                 return e.value;
         }
         return null;
     }
     private V getForNullKey() {
         for (Entry<K,V> e = table[0]; e != null; e =  e.next) {
             if (e.key == null)
                 return e.value;
         }
         return null;
     }
     public V put(K key, V value) {
         if (key == null)
             return putForNullKey(value);
         int hash = hash(key.hashCode());
         int i = indexFor(hash, table.length);
         for (Entry<K,V> e = table[i]; e != null; e =  e.next) {
             Object k;
             if (e.hash == hash && ((k = e.key) ==  key || key.equals(k))) {
                 V oldValue = e.value;
                 e.value = value;
                 e.recordAccess(this);
                 return oldValue;
             }
         }
         modCount++;
         addEntry(hash, key, value, i);
         return null;
     }
     private V putForNullKey(V value) {
         for (Entry<K,V> e = table[0]; e != null; e =  e.next) {
             if (e.key == null) {
                 V oldValue = e.value;
                 e.value = value;
                 e.recordAccess(this);
                 return oldValue;
             }
         }
         modCount++;
         addEntry(0, null, value, 0);
         return null;
     }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.