(首次添加--首次添加--首次添加)
代码实现
import java.util.HashSet;
public class SetDemo {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("abc");//集合中传入元素“abc”
System.out.println(set);//输出集合中的元素
}
}
源代码详解
①//从这里可以看出HashSet的add方法其实调用的是map的put方法
public boolean add(E e) {//e为我们传入的字符串“abc”
return map.put(e, PRESENT)==null;
}
②//查看map的put方法
public V put(K key, V value) {//此时key就变成了我们传入的字符串“abc”
return putVal(hash(key), key, value, false, true);
}
③//这个方法为put方法中,计算hash值的方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//该方法返回newTab;//返回newTab的地址
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
//hash为Hash方法计算字符串的值,key为传入的“abc”,value=PRESENT常量。
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0) {//此时节点Node中没有元素,table=null,该方法成立,执行
n = (tab = resize()).length;//resize()方法下个代码块查看
//此时table=tab,指向同一个地址
}
if ((p = tab[i = (n - 1) & hash]) == null) {//第一次存入数据,数组为空,该方法成立,于是就将我们的“abc”存到了数组中,i = (n - 1) & hash,添加成功
tab[i] = newNode(hash, key, value, null);
---------------------------第一次添加成功,此时返回put方法------------------------------
}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;
}
resize()方法返回的是数组的地址
⑤//该方法返回newTab;//返回newTab的地址
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;//此时table为空
int oldCap = (oldTab == null) ? 0 : oldTab.length;//通过三目运算得出oldCap=0
int oldThr = threshold;//threshold为全局变量,默认值为0,所以此时oldThr=0
int newCap, newThr = 0;
if (oldCap > 0) {//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) { //oldThr=0,不成立不执行
newCap = oldThr;
}else { //执行这一步
newCap = DEFAULT_INITIAL_CAPACITY;//该常量=16,即newCap=16
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);//此时newThr也被定义为一个数,数值为后面常量计算的值(后面用不到,不要做过多考虑)
}
if (newThr == 0) {//此时newThr不为0,该判断错误,不执行
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE);
}
threshold = newThr;//将newThr的值赋值给threshold
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//此时newTab存储数组的地址
table = newTab;//table也指向新的数组;以下代码此次运行条件不满足,直接运行最后一步
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;//返回newTab的地址
}