以add()方法为例,查看hashSet的底层源码实现,后面的源码啃不动了。。。。就我理解是数组+链表;当链表结构达到8个时候,会将前面的8个链表转换成二叉树结构,而不是以第8个链表为根节点,往后依次形成二叉树,即将数组+链表变成了数组+二叉树,所以最终的结构可能是:数组+链表+二叉树,其中二叉树以数组为基础,而不是以链表为基础,即不会在链表后面形成二叉树,而是将链表(达到8个结点)转换成二叉树。
public boolean add(E e) {
//第一次返回null,即第一次添加成功
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
hash(key)方法:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
hashCode()方法:
//调用非java接口
//此处调用的object的原生hashCode()方法
public native int hashCode();
transient Node<K,V>[] table;
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//初始化table为null
if ((tab = table) == null || (n = tab.length) == 0)
//resize()方法扩容,返回Node[],数组大小是16
n = (tab = resize()).length;
//以下判断简写成15&hash,进一步写成:15 & ((hx = ((Object)obj).hashCode()) ^ (hx >>> 16))
//hx是一个int类型的临时变量,obj是任意类型,基本或引用数据类型
//15 & ((hx = ((Object)obj).hashCode()) ^ (hx >>> 16))
//等价于
//((Object)obj).hashCode() % 16;即tab[]数组索引在0~15之间
//用Object强转是因为其调用的是Object原生的hashCode()方法,而非其他重写的hashCode()方法
//此处if判断,正是避免重复元素的关键,相同元素不会进行newNode
if ((p = tab[i = (n - 1) & hash]) == null)
//返回一个Node对象,其值是传入的key,Node对象作为Node<K,V>[]中的第i个元素
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//对于Set集合来说,判断是否是同一个元素
//对于Map集合来说,判断key是否相同
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
//此处的p既是Node<K,V>[]中已存在的node对象
e = p;
//对于Set而言,判断元素是否是二叉树
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
//对于Set集合而言,创建链表结构
for (int binCount = 0; ; ++binCount) {
//p是数组中存储的Node元素,同时也是链表中的结点元素
if ((e = p.next) == null) {
//新创建一个Node对象,并且作为上一个Node对象的下一个元素,即链表结构
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;
}
}
//对于Set集合而言,同一个元素,返回一个Object地址
//对于Map而言,判断Map中key是否存在,新值覆盖旧值
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
//新值覆盖原来的值
e.value = value;
afterNodeAccess(e);
//返回旧值
return oldValue;
}
}
++modCount;
//threshold阈值,刚开始是16*0.75=12
if (++size > threshold)
//当添加完第12个元素后进行扩容,同时将原来的Node<K,V>[]数组中的元素全部添加到新的数组中(32)
resize();
afterNodeInsertion(evict);
//每次添加新元素都返回Null
return null;
}
final Node<K,V>[] resize() {
//开始初始化为null
//在添加一个元素后,table不为null,大小是16
Node<K,V>[] oldTab = table;
//16
int oldCap = (oldTab == null) ? 0 : oldTab.length;
//开始初始化为0
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//newCap = 32,容量扩大一倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
//阈值扩大一倍,24
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else {
//下面的英文解释初始化为0,使用默认值
// zero initial threshold signifies using defaults
//static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
newCap = DEFAULT_INITIAL_CAPACITY;//16
//static final float DEFAULT_LOAD_FACTOR = 0.75f;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//12
}
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"})
//第一次添加元素时创建大小是16的Node数组
//扩容时创建大小是32的Node<K,V>[]数组
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
//将新的Node<K,V>[]数组赋给成员变量table
table = newTab;
if (oldTab != null) {
//将原Node<K,V>[]数组中的元素全部放入新的newTab中
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
//获取原数组中的node对象赋给e
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
//判断是否是数组,e.next == null 代表数组
if (e.next == null)
//同上15&hash,等价于对32取模,结果是0~31,将e放入新的Node<K,V>[]数组中
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;
}
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
//扩容后大小是64
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
//将链表的元素转换成树结构
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
replacementTreeNode()方法:
TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
return new TreeNode<>(p.hash, p.key, p.value, next);
}
treeify()方法:
final void treeify(Node<K,V>[] tab) {
TreeNode<K,V> root = null;
for (TreeNode<K,V> x = this, next; x != null; x = next) {
next = (TreeNode<K,V>)x.next;
x.left = x.right = null;
if (root == null) {
x.parent = null;
x.red = false;
root = x;
}
else {
K k = x.key;
int h = x.hash;
Class<?> kc = null;
for (TreeNode<K,V> p = root;;) {
int dir, ph;
K pk = p.key;
if ((ph = p.hash) > h)
dir = -1;
else if (ph < h)
dir = 1;
else if ((kc == null &&
(kc = comparableClassFor(k)) == null) ||
(dir = compareComparables(kc, k, pk)) == 0)
dir = tieBreakOrder(k, pk);
TreeNode<K,V> xp = p;
if ((p = (dir <= 0) ? p.left : p.right) == null) {
x.parent = xp;
if (dir <= 0)
xp.left = x;
else
xp.right = x;
root = balanceInsertion(root, x);
break;
}
}
}
}
moveRootToFront(tab, root);
}