HashMap1.8结构笔记

1  初始容量 16
   static final int DEFAULT_INITIAL_CAPACITY = 1 << 4

2 容量最大值 1073741824

static final int MAXIMUM_CAPACITY = 1 << 30;

3 加载因子

static final float DEFAULT_LOAD_FACTOR = 0.75f;

4 当链表长度大于8的时候将链表转化为红黑树

static final int TREEIFY_THRESHOLD = 8;

5 当链表长度小于6的时候将红黑树转化为链表

static final int UNTREEIFY_THRESHOLD = 6;

6 当数组容量大于或者等于64的时候,并且链表长度大于4的时候,将链表转化为红黑树

static final int MIN_TREEIFY_CAPACITY = 64;

7 静态内部类,使用到了单向链表,保存了hash值(目的是为了下次扩容的时候,能够计算key在新的数组中的index值),键值对就存在这个静态内部类中

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

    public final K getKey()        { return key; }
    public final V getValue()      { return value; }
    public final String toString() { return key + "=" + value; }

    public final int hashCode() {
        return Objects.hashCode(key) ^ Objects.hashCode(value);
    }

    public final V setValue(V newValue) {
        V oldValue = value;
        value = newValue;
        return oldValue;
    }

    public final boolean equals(Object o) {
        if (o == this)
            return true;
        if (o instanceof Map.Entry) {
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
            if (Objects.equals(key, e.getKey()) &&
                Objects.equals(value, e.getValue()))
                return true;
        }
        return false;
    }
}

8 table数组,类型为单向链表

transient Node<K,V>[] table;

9   HashMap的size

transient int size;

10  遍历集合的时候,防止多线程篡改数据

transient int modCount;

11 加载因子

final float loadFactor;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值