HashMap源码简略分析

HashMap中核心的成员变量

  1. 初始化桶大小,即数组默认大小。
  2. 桶最大值,即数组最大值。
  3. 默认的负载因子(0.75)
  4. Table真正存放数据的数组
  5. Map存放数量的大小
  6. 桶大小,可在初始化时显式指定
  7. 负载因子,可在初始化时显式指定。
Static final int MAXIMUM_CAPACITY = 1 << 30; 最大容量

Static final float DEFAULT_LOADER_FACTOR = 0.75f 默认负载因子

Static final int TREEIFY_THRESHOLD = 8 用于判断是否需要将链表转换为红黑树的阈值

Transient Node<K,V>[] table;

Transient Set<Map.Entry<K,V>> entrySet;

Transient int size map中含有的键值对个数



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) //根据当前key的hashcode值定位某个桶p,判空
        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))))

//将当前桶的hash值和key值与要加入的键值对的hash/key值进行对比,如果相等,将当前桶第一个节点赋值给e。
            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;//e在找尾节点过程中每经过一个节点,都会将其和传入参数进行对比。如果hash,key值相等,则桶内该节点和要插入节点重复,直接跳出循环。
                p = e;//找尾节点过程中,p向后移
            }
        }
        if (e != null) {//如果在桶链表尾插入节点了,则e指空。其他情况都是节点已存在
            V oldValue = e.value;
            if (!onlyIfAbsent || oldValue == null)
                e.value = value;//更新已存在节点的value为传入的value
            afterNodeAccess(e);
            return oldValue;
        }
    }
    ++modCount;//map修改次数加一
    if (++size > threshold)
        resize();
    afterNodeInsertion(evict);
    return null;
}





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 && //如果当前map数组不为空
        (first = tab[(n - 1) & hash]) != null) {//且根据hash和key定位的某个桶内不为空
        if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k))))
            return first;//如果第一个节点的hash和key与参数相同,直接返回第一个节点
        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;
}

 

HashMap扩容调用resize()方法时会死循环。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值