189 8069 5689

HashMap源码解析

/*

*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;//16 默认初始化容量为16
static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认负载因子
static final int TREEIFY_THRESHOLD = 8;//树化阀值
static final int UNTREEIFY_THRESHOLD = 6;//非树化阀值
static final int MIN_TREEIFY_CAPACITY = 64;//最小树化容量

创新互联公司坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。十多年网站建设经验创新互联公司是成都老牌网站营销服务商,为您提供成都网站制作、成都网站设计、网站设计、H5开发、网站制作、品牌网站设计、小程序设计服务,给众多知名企业提供过好品质的建站服务。

transient Node[] table;//存储元素数组
transient Set> entrySet;
transient int size;//元素个数
transient int modCount;//修改次数
int threshold;//阀值
final float loadFactor;//负载因子
/*
Node内部类,元素key-value
*/
static class Node implements Map.Entry {
final int hash;
final K key;
V value;
Node next;

    Node(int hash, K key, V value, Node next) {
        this.hash = hash;
        this.key = key;
        this.value = value;
        this.next = next;
    }
 } 

//求hash值 大于2^16 65536的hash值有所改变
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//默认构造函数
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // 负载因子默认0.75
}
//put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//put方法调用:hash 为key的hash值,onlyIfAbsent为fasle,evict为false
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node[] tab; Node p; int n, i;
//存储元素数组为空并且长度为0
if ((tab = table) == null || (n = tab.length) == 0)
//进行扩容 tab为扩容后的数组 n为扩容后的长度
n = (tab = resize()).length;
//m%n和m&n 当n满足2^k,是相等的
//p为table存在的同一索引的元素
//p为空,进行新增
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
//p不为空,进行链表添加
Node e; K k;
//p的hash值等于put的hash值 并且p的key值等于put的key值或者key不为空并put的key值等于p的key值
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//p赋值给e
e = p;
//p为TreeNode实例 红黑树节点
else if (p instanceof TreeNode)
//p putTreeVal为TreeNode 返回
e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);
else {
// p其他
//单链表循环处理
for (int binCount = 0; ; ++binCount) {
//单链表只有一个头节点,直接新建一个节点
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//当大于等于树化阀值8,将单链表转红黑树
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;
                p = e;
            }
        }
        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)
        //扩容2倍,重新调整数组
        resize();
    afterNodeInsertion(evict);
    return null;
}     

//putVal方法调起:当存储元素数组为空并且长度为0
final Node[] resize() {
Node[] 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 扩容2倍
}
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) {//临界值还为0,则设置临界值
float ft = (float)newCap
loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;@SuppressWarnings({"rawtypes","unchecked"})
br/>@SuppressWarnings({"rawtypes","unchecked"})
[] newTab = (Node[])new Node[newCap];
table = newTab;
//链表拆分
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node 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)e).split(this, newTab, j, oldCap);
else { // preserve order
Node loHead = null, loTail = null;
Node hiHead = null, hiTail = null;
Node next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
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;
}


本文名称:HashMap源码解析
网站URL:http://cdxtjz.com/article/igipid.html

其他资讯