java源码_HashMap(一)

哈希表采用数组+链表或红黑树的方式存储,查询效率高

1.重要的成员变量

/**
 * The default initial capacity - MUST be a power of two.
 */
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 默认的数组容量
/**
 * The load factor used when none specified in constructor.
 */
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认的负载因子
/**
 * The table, initialized on first use, and resized as
 * necessary. When allocated, length is always a power of two.
 * (We also tolerate length zero in some operations to allow
 * bootstrapping mechanics that are currently not needed.)
 */
transient Node<K,V>[] table;//数组容器
/**
 * Holds cached entrySet(). Note that AbstractMap fields are used
 * for keySet() and values().
 */
transient Set<Map.Entry<K,V>> entrySet;//存放Entry的Set
/**
 * The number of key-value mappings contained in this map.
 */
transient int size;//数量
/**
 * The number of times this HashMap has been structurally modified
 * Structural modifications are those that change the number of mappings in
 * the HashMap or otherwise modify its internal structure (e.g.,
 * rehash).  This field is used to make iterators on Collection-views of
 * the HashMap fail-fast.  (See ConcurrentModificationException).
 */
transient int modCount;结构性改变的数量,为了fail-fast
/**
 * The next size value at which to resize (capacity * load factor).
 *
 * @serial
 */

int threshold;//扩容阀值 = 容量 * 负载因子
/**
 * The load factor for the hash table.
 *
 * @serial
 */
final float loadFactor;//负载因子

2.构造函数,主要是设定容量和负载因子

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);
    this.loadFactor = loadFactor;
    this.threshold = tableSizeFor(initialCapacity);//设立阀值大小,并没有初始化table
}

public HashMap(int initialCapacity) {
    this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public HashMap(Map<? extends K, ? extends V> m) {
    this.loadFactor = DEFAULT_LOAD_FACTOR;
    putMapEntries(m, false);
}
保证容量是2的幂的方法

/**
 * Returns a power of two size for the given target capacity.
 */
static final int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}

3.hash方法

static final int hash(Object key) {//扰动函数,高位也参与运算
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//高16位和低16位异或运算,主要是为了高位也参与
}

计算table索引位置的方法

( table.length - 1) & hash

因为table.length是2的整数幂,所以table.length-1是奇数,二进制所有位是1,比如15是00001111,hash如果直接与之异或运算,则只有最后几位参与运算,则碰撞概率高,通过扰动函数,减少碰撞

另外容量是2的幂次方,resize的时候更快,散列化程度更高

4.resize方法:一要求出新的容量,二求出新的阀值,三如果需要扩容

/**
 * Initializes or doubles table size.  If null, allocates in
 * accord with initial capacity target held in field threshold.
 * Otherwise, because we are using power-of-two expansion, the
 * elements from each bin must either stay at same index, or move
 * with a power of two offset in the new table.
 *
 * @return the table
 */
final Node<K,V>[] resize() {
    Node<K,V>[] oldTab = table;
    int oldCap = (oldTab == null) ? 0 : oldTab.length;//老的容量,0或者2的幂次方
    int oldThr = threshold;//老的阀值,0或者oldCap*factor
    int newCap, newThr = 0;//新的容量和新的阀值
    if (oldCap > 0) {//老的容量>0
        if (oldCap >= MAXIMUM_CAPACITY) {//老的容量到上限
            threshold = Integer.MAX_VALUE;
            return oldTab;
        }
        else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                 oldCap >= DEFAULT_INITIAL_CAPACITY)
            newThr = oldThr << 1; // double threshold
    }
    else if (oldThr > 0) //如果老的容量是0,则用初始化时候的阀值做容量,延迟初始化
        newCap = oldThr;
    else {//如果老容量和老的阀值都是0,则都取默认值
        newCap = DEFAULT_INITIAL_CAPACITY;
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
    }
    if (newThr == 0) {
        float ft = (float)newCap * loadFactor;//新容量*负载因子
        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?//如果新容量和ft都小于最大值则新阀值=ft
                  (int)ft : Integer.MAX_VALUE);
    }
    threshold = newThr;
    @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//新建table数组
    table = newTab;
    if (oldTab != null) {
        for (int j = 0; j < oldCap; ++j) {//把老的table数组复制到新的table数组
            Node<K,V> e;
            if ((e = oldTab[j]) != null) {//索引为i的数据不为null则进入复制
                oldTab[j] = null;//释放老数组的引用
                if (e.next == null)//如果头结点后没有了,则复制一个就够了
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)//如果是红黑树结构,则调用红黑树的拆分方法
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
                else { // preserve order
                    Node<K,V> loHead = null, loTail = null;//老的索引位置的头尾节点,low index
                    Node<K,V> hiHead = null, hiTail = null;//新的索引位置的头尾节点,high
                    Node<K,V> next;
                    do {
                        next = e.next;
                        if ((e.hash & oldCap) == 0) {//oldCap是1000的二进制,其实就是看e的hash的在oldCap的为1这一位上是0还是1

                            if (loTail == null)
                                loHead = e;
                            else
                                loTail.next = e;
                            loTail = e;
                        }
                        else {//否则移到新的索引位置 high index
                            if (hiTail == null)
                                hiHead = e;
                            else
                                hiTail.next = e;
                            hiTail = e;
                        }
                    } while ((e = next) != null);
                    if (loTail != null) {
                        loTail.next = null;
                        newTab[j] = loHead;
                    }
                    if (hiTail != null) {
                        hiTail.next = null;
                        newTab[j + oldCap] = hiHead;
                    }
                }
            }
        }
    }
    return newTab;
}

发布了138 篇原创文章 · 获赞 10 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/idealemail/article/details/80406010