hash java相關源碼簡析

原理

hashtable底層不保證物理內存順序,實現插入,查找和刪除時間複雜度O(1),這三種操做都是基於模仿了數組的隨機訪問:java

  1. 數組隨機訪問,經過數組下標index訪問value,index-->內存地址-->value
  2. hashtable查找,經過關鍵字key訪問value,key-->hashfun(key)-->index-->內存地址-->value

因此hash操做時間複雜度的關鍵是hashfun(key)時間複雜度,另外這個hashfun也部分決定了衝突發生的機率。node

java源碼LinkedHashSet LinkedHashMap分析

java的LinkedHashSet繼承HashSet,而HashSet存儲數據用了HashMap,key-value的value使用了算法

private static final Object PRESENT = new Object()

java的LinkedHashMap繼承HashMap。
因此LinkedHashSet LinkedHashMap的hash函數都在HashMap裏面。數組

java源碼HashMap分析

存儲值app

Node<K,V>[] table //真正存儲的地方

value類型node,除了存儲開發者關注的value外,還保存了hash值,以及hash衝突時節點next(源碼的不少處使用TreeNode,未分析)函數

static class Node<K,V> implements Map.Entry<K,V> {
    final int hash;
    final K key;
    V value;
    Node<K,V> next;

    Node(int hash, K key, V value, Node<K,V> next) {
        this.hash = hash;
        this.key = key;
        this.value = value;
        this.next = next;
    }
    ...
}

查找,只給開發者返回value,屏蔽底層node的其餘信息,根據hashfun計算index:(n - 1) & hashthis

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
}

final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
}

插入,更新code

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
}

源碼爲了計算效率,大量使用了位運算符,賦值表達式返回值繼承

hash算法

hash算法調用了object.hashCode,一個原生native方法,具體c源碼不討論。內存

相關文章
相關標籤/搜索