Java容器解析系列(14) IdentityHashMap详解

IdentityHashMap,使用什么的跟HashMap相同,主要不同点在于:

  1. 数据结构:使用一个数组table来存储 key:value, table[2k]key, table[2k + 1]value,也即:
    key:value ==> table[2k]:table[2k + 1](HashMap使用数组 + 链表);
  2. IdentityHashMap 中的 keyvalue 通过 ==来比较是否相等(HashMap通过equals());
  3. IdentityHashMap 中的 hash冲突解决方式为线性探测法(HashMap拉链法);

具体,我们来看关键源码:

/**
 * 数据存储结构:
 * 使用一个数组table来存储 key - value,第 table[2k] 为key, table[2k + 1] 为value,也即:
 * key:value ==> table[2k]:table[2k + 1]
 * IdentityHashMap 中的 key 和 value 通过 "==" 来比较是否相等(HashMap通过equals()来比较是否相等)
 * @since 1.4
 */
public class IdentityHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, java.io.Serializable, Cloneable {

    private static final int DEFAULT_CAPACITY = 32;
    private static final int MINIMUM_CAPACITY = 4;
    private static final int MAXIMUM_CAPACITY = 1 << 29;
    private transient Object[] table;// 存储键值对的数组
    private int size;
    private transient int modCount;
    private transient int threshold;
    private static final Object NULL_KEY = new Object();

    // 如果key为null,使用NULL_KEY代替
    private static Object maskNull(Object key) {
        return (key == null ? NULL_KEY : key);
    }

    // 如果之前key为null,被替换为NULL_KEY,现在替换回来
    private static Object unmaskNull(Object key) {
        return (key == NULL_KEY ? null : key);
    }

    public IdentityHashMap() {
        init(DEFAULT_CAPACITY);
    }

    // 各种构造器方法,省略......

    // 构造器中都会调用该方法
    private void init(int initCapacity) {
        threshold = (initCapacity * 2) / 3;
        // 因为键值存储在同一个数组中,所有数组大小为初始容量的2倍
        table = new Object[2 * initCapacity];
    }

    private static int hash(Object x, int length) {
        // 使用System.identityHashCode(x)计算hash值
        int h = System.identityHashCode(x);
        // 扰动;并且该表达式保证了元素的散列值是偶数
        return ((h << 1) - (h << 8)) & (length - 1);
    }

    // hash冲突解决方式:线性探测法(linear-probe);因为key后面立马是value,这里线性探测每次递增2;
    // 并且这里实现了循环探测;
    private static int nextKeyIndex(int i, int len) {
        return (i + 2 < len ? i + 2 : 0);
    }

    public V get(Object key) {
        Object k = maskNull(key);
        Object[] tab = table;
        int len = tab.length;
        int i = hash(k, len);
        while (true) {
            Object item = tab[i];
            if (item == k)
                return (V) tab[i + 1];// 如果第i个位置存储为key,第i+1位置则存储为对应的value
            if (item == null)
                return null;
            // 线性探测
            i = nextKeyIndex(i, len);
        }
    }

    public boolean containsValue(Object value) {
        Object[] tab = table;
        for (int i = 1; i < tab.length; i += 2)
            // value也使用"=="判断相等
            if (tab[i] == value && tab[i - 1] != null)
                return true;
        return false;
    }

    public V put(K key, V value) {
        // 这里必须为 null 提供mask,因为在进行遍历,get()等操作时,如果遇到key为null,则认为该位置没有键值对
        Object k = maskNull(key);
        Object[] tab = table;
        int len = tab.length;
        int i = hash(k, len);
        Object item;
        // key为null,则认为该位置没有键值对
        while ((item = tab[i]) != null) {
            if (item == k) {// 通过"=="判断key相同
                V oldValue = (V) tab[i + 1];
                tab[i + 1] = value;
                return oldValue;
            }
            i = nextKeyIndex(i, len);// 线性探测
        }
        modCount++;
        // 如果第i个位置存储为key,第i+1位置则存储为对应的value
        tab[i] = k;
        tab[i + 1] = value;
        if (++size >= threshold)
            resize(len); // len == 2 * current capacity.
        return null;
    }

    // 每次扩容为原来的2倍
    private void resize(int newCapacity) {
        int newLength = newCapacity * 2;
        Object[] oldTable = table;
        int oldLength = oldTable.length;
        if (oldLength == 2 * MAXIMUM_CAPACITY) { // can't expand any further
            if (threshold == MAXIMUM_CAPACITY - 1)
                throw new IllegalStateException("Capacity exhausted.");
            threshold = MAXIMUM_CAPACITY - 1; // Gigantic map!
            return;
        }
        if (oldLength >= newLength)
            return;
        Object[] newTable = new Object[newLength];
        threshold = newLength / 3;
        // 对每个键值对重新进行散列到新数组中
        for (int j = 0; j < oldLength; j += 2) {
            Object key = oldTable[j];
            if (key != null) {
                Object value = oldTable[j + 1];
                oldTable[j] = null;
                oldTable[j + 1] = null;
                int i = hash(key, newLength);
                while (newTable[i] != null)
                    i = nextKeyIndex(i, newLength);
                newTable[i] = key;
                newTable[i + 1] = value;
            }
        }
        table = newTable;
    }

    public V remove(Object key) {
        Object k = maskNull(key);
        Object[] tab = table;
        int len = tab.length;
        int i = hash(k, len);

        while (true) {
            Object item = tab[i];
            if (item == k) {
                modCount++;
                size--;
                V oldValue = (V) tab[i + 1];
                tab[i + 1] = null;
                tab[i] = null;
                // 第i个位置的键值对移除了,空出了位置,看后面是否有键值对要填补这个位置(有些键值对是因为线性探测导致其位置偏离了其hash值)
                closeDeletion(i);
                return oldValue;
            }
            if (item == null)
                return null;
            i = nextKeyIndex(i, len);
        }

    }

    // 指定位置的键值对移除了,空出了位置,看后面是否有键值对要填补这个位置(有些键值对是因为线性探测导致其位置偏离了其hash值)
    private void closeDeletion(int d) {
        // Adapted from Knuth Section 6.4 Algorithm R
        Object[] tab = table;
        int len = tab.length;

        Object item;
        // d:空出来的键值对位置
        // i:当前处理的键值对位置
        // 直到key==null时才退出循环
        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null; i = nextKeyIndex(i, len)) {
            int r = hash(item, len);
            // i != r:证明item是被线性探测后放置在位置i;

            // 这种情况 在插入时,线性探测出现了循环
            // i<r:item通过线性探测到前面位置r前面去了
            // ___i____r_____d____ i<r<=d
            // ___d____i_____r____ d<=i<r
            // 这个表达式相当于 (i < r) && !(i < d && d < r),也即   __i__d__r__(这种情况不能处理,因为这种情况下,因为线性探测是向后循环探测)
            if ((i < r && (r <= d || d <= i))
                    ||
                    (r <= d && d <= i)// 表示情况:___r____d____i____,建议先理解这种更简单的情况
            ) {
                // 把item对应的键值对放置到位置d;位置i为新空出来的键值对位置;
                tab[d] = item;
                tab[d + 1] = tab[i + 1];
                tab[i] = null;
                tab[i + 1] = null;
                d = i;
            }
        }
    }

    public void clear() {
        modCount++;
        Object[] tab = table;
        for (int i = 0; i < tab.length; i++)
            tab[i] = null;
        size = 0;
    }
    
    // Object方法代码,省略
    // 常用Map代码,省略
    // 序列化相关代码,省略

}
原文地址:https://www.cnblogs.com/jamesvoid/p/10937446.html