1. 程式人生 > >初學hashmap資料結構

初學hashmap資料結構

一、hashmp引數介紹

static final int DEFAULT_INITIAL_CAPACITY = 16; //預設初始化集合大小
static final int MAXIMUM_CAPACITY = 1 << 30;//設定集合的最大值   相當於2的30次方
static final float DEFAULT_LOAD_FACTOR = 0.75f; // 擴充套件因子,當集合已經被使用到0.75時對集合擴充套件

二、方法介紹

//hashmap構造方法
public HashMap(int initialCapacity, float loadFactor) {
//初始化容量不能小於0
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
   //預設從1開始初始化集合,當小於構造方法傳來的集合容量大小時,進行移位操作,找到剛好大於初始化的容量的值,一般都為2的N次方
    int capacity = 1;
    while (capacity < initialCapacity)
        capacity <<= 1;
    this.loadFactor = loadFactor;
    //算出集合下一次需要初始化的大小
    threshold = (int)(capacity * loadFactor);
    //構建出一個大小為capacity陣列表
    table = new Entry[capacity];
   //程式預留方法,方便後續擴充套件,這個操作在spring繼承封裝serlvet哪裡體現的淋漓盡致
    init();
}

get方法

 public V get(Object key) {
    if (key == null)
        return getForNullKey();
    //根據傳來的key,計算hashcode值      
    int hash = hash(key.hashCode());
    for (Entry<K,V> e = table[indexFor(hash, table.length)];
         e != null;
         e = e.next) {
        Object k;
        //當hash值一樣,且物件一樣時,返回他的值, 這個判斷提現hash自反性
        if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
            return e.value;
    }
    return null;
}

//將補充雜湊函式應用於給定的雜湊程式碼防範低質量的雜湊函式。這是至關重要的因為
HashMap使用兩倍長度的雜湊表否則,將遇到沒有區別的雜湊碼的衝突在較低的部分。注意:Null鍵總是對映到hash 0,因此索引0
static int hash(int h) {
這個函式確保雜湊程式碼的不同每個位位置的常數倍數都有一個界碰撞次數(預設負載係數為8次)。
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
//返回雜湊碼h的索引
static int indexFor(int h, int length) {
return h & (length-1);
}

put方法

public V put(K key, V value) {
    if (key == null)
        return putForNullKey(value);
     //計算key值的hashcode
    int hash = hash(key.hashCode());
    //計算出在table的索引位置
    int i = indexFor(hash, table.length);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        //解決hash衝突  用久的值替換新的值
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }
  //記錄hashmap資料結構被改變的次數
    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

void addEntry(int hash, K key, V value, int bucketIndex) {
Entry<K,V> e = table[bucketIndex];
//重新排序這個table
table[bucketIndex] = new Entry<>(hash, key, value, e);
//本次新增後的值如果大於擴容值,進行2倍數擴容
if (size++ >= threshold)
resize(2 * table.length);
}