源码为android-28 sdk中
1、构造方法
无参构造,只是给了缩放赋值,默认值0.75
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
带有初始大小的构造方法:
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
带有初始化大小和缩放因子的构造方法:
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);
}
初始大小校验,大小为int值,所以不能超过最大值(1 << 30);缩放因子校验;threshold 是扩容阈值(它在此时的计算结果并不是直接拿来当阈值的,下面put方法会涉及)
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;
}
此方法,计算了大于cap的,是2的次方的值;这个算法的前提是:cap为正数,cap转化为二进制,我们假设数未转换前从左到右,第一个1为1位,往右依次为次1位,次次1位;那么n |= n >>> 1,就会使n的1位与次一位都为1(也许之前的次1位不为1),n >>> 2的结果,就是cap 次次1位,次次次1位都为1,与n求与,那么就是n的1位-次次次1位都位1,一次类推,一直到 n>>> j == 0时,n的从1位到右边最后一位都是1;
2、插入值
插入值对
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
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;
if ((p = tab[i = (n - 1) & hash]) == null)
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))))
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;
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)
resize();
afterNodeInsertion(evict);
return null;
}
内部变量table为空,也就是第一次进入的执行动作是
Node<K,V>[] tab; Node<K,V> p; int n, i;
n = (tab = resize()).length;
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
resize 为扩容方法,后续会讲;后续插入存在两种情况:如果tab[i = (n - 1) & hash]) == null也就是hash值对应的值为空,直接设置,如果不为空,则放入桶中:此处可以大致看出数据结构:
- HashMap其实数据存储结构为 数组+ 链表
- 数组中数据,是键值的hash值 & (n -1)为索引;n为数组的实际大小(2幂次)
- 数组中,如果索引i有值,再次加入时,数据中的数据为链表头,其它加入后面
而n值的结果,和扩容均在resize方法;
3、扩容
resize 方法
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) {
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;
}
从此方法中可以看出,有三种情况
1、构造函数时,无参,此时阈值threshold初始为0,那么
会赋值为 1<< 4(16), 然后 newCap = threshold, threshold 按照缩放因子(默认为0.75)* threshold;newCap用来作为table数组大小,并进行返回;数组大小为2的次幂
2、如果参数传了,threshold已经为2的次幂,赋值给newCap, threshold 按照缩放因子(默认为0.75)* threshold;newCap用来作为table数组大小,并进行返回;数组大小为2的次幂
3、如果数组不为空,threshold大小为数组大小 * 缩放因子;那么数组大小和threshold都会左移一位(数据不溢出);这时所有数据都要重新计算数组位置,和链表位置;
4、算法
1、数组的存储基于计算排序的原理;数组大小是2的次幂;利用hash值与数组大小-1的与结果作为索引
2、链表存储结构:单链表 + 红黑树
*** 红黑树
是一颗,二叉搜索树,并且满足,
- 每个节点是黑树或者红树
- 根节点必须是黑叔
- 树的路径所有黑树的个数相同
- 红树的子节点必须是黑树
因此,其也是自平衡的,任意一个节点的左右孩子子树层级不会超过2
其查找,和二叉搜索树类似;其添加、删除和平衡二叉树类似(都涉及左旋,右旋操作)
网友评论