HashMap

HashMap

  • 结构: 数组+链表(红黑树)

  • 放数据:

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

  • key的hash(): key==null,其hash为0,不为null其值为key的高16位异或key的低16位(h右移位16的值),这样会把这个hashcode的位的用上,因为接下来通过hash值判断数组的位置时使用的是01111这样的与运算,如果直接使用hashcode会使得hashcode的高位没有意义

    hashcode与equals的关系: equals相等则hashcode一定相等,反之不一定,因为两个key相同的键值对数据就是hashcode相同但是equals不同

    static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

  • 链表节点:

    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;
    }

  • 数组大小: 默认16,最大值1 << 30

    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
    static final int MAXIMUM_CAPACITY = 1 << 30;

  • 数组扩容: 条件: 当前数组的容量被使用了0.75倍之后进行2倍扩容

    static final float DEFAULT_LOAD_FACTOR = 0.75f;

  • 链表转变: 当链表节点增加大于等于8时转换成红黑树,减少到6时有红黑树转换成普通链表

    static final int TREEIFY_THRESHOLD = 8;
    static final int UNTREEIFY_THRESHOLD = 6;

  • putVal

    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;//通过resize()初始化或者加倍数组大小

    //节点为空
    if ((p = tab[i = (n - 1) & hash]) == null)//用01111与hash进行与运算(计算机执行比较快,目的与取模一样)
    tab[i] = newNode(hash, key, value, null);//当该数组位置没有元素,直接把Node节点赋给该数组位置

    //节点非空
    else {
    Node<K,V> e; K k;

    //添加元素的key == p.key
    if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
    e = p;

    //添加元素的key != p.key,放入红黑树
    else if (p instanceof TreeNode)
    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

    //添加元素的key != p.key,放入普通链表
    else {
    for (int binCount = 0; ; ++binCount) {
    if ((e = p.next) == null) {
    p.next = newNode(hash, key, value, null);
    if (binCount >= TREEIFY_THRESHOLD - 1) // 节点大于等于8时由链表转换为红黑树
    treeifyBin(tab, hash);
    break;
    }
    if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))//遍历链表时,存在相同key节点
    break;
    p = e;
    }
    }
    //上面的for循环如果发现相同的key的节点就把e置为改节点,否则置为null
    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)//扩容: size为数组所使用的大小,threshold为0.75*数组容量
    resize();
    afterNodeInsertion(evict);
    return null;

    为什么数组两倍扩容? ---->因为需要使用数组长度(n)-1进行与计算,需要生成后位都是1的数,初始容量为16(10000),两倍扩容后就可以产生这个效果(例:10000-1=01111,100000000-1=011111111)

  • 初始化或者加倍数组大小

    final Node<K,V>[] resize() {
    Node<K,V>[] oldTab = table;
    int oldCap = (oldTab == null) ? 0 : oldTab.length;
    int oldThr = threshold;
    int newCap, newThr = 0;
    if (oldCap > 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) // initial capacity was placed in threshold
    newCap = oldThr;
    else { // zero initial threshold signifies using defaults
    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 ?
    (int)ft : Integer.MAX_VALUE);
    }
    threshold = newThr;
    @SuppressWarnings({"rawtypes","unchecked"})
    Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
    table = newTab;
    if (oldTab != null) {
    //遍历数组
    for (int j = 0; j < oldCap; ++j) {
    Node<K,V> e;
    if ((e = oldTab[j]) != 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;
    Node<K,V> hiHead = null, hiTail = null;
    Node<K,V> next;
    do {
    next = e.next;
    if ((e.hash & oldCap) == 0) {//链表节点迁移只有两种可能,原地不动和往后移oldCap
    if (loTail == null)
    loHead = e;
    else
    loTail.next = e;
    loTail = e;
    }
    else {
    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;
    }

    数组扩容后节点迁移

    扩容前: 0x0101 & 01111 = 0101

    扩容后: 0x0101 & 11111 = x0101

    x只能为0或1,当为0时节点保持不动,当为1时,节点位置增加了10000也就是oldCap(扩容前数组的容量)

  • 设置初始容量

    initialCapacity = (存储元素个数 / 负载因子) + 1

  • null值(<<阿里巴巴Java开发手册>>)

posted @ 2018-11-24 19:35  原来是这样啊  阅读(163)  评论(0)    收藏  举报