HashSet的add添加方法的判读(String类型)
String类型添加操作判读
public static void main(String[] args) {
Set<String> objects = new HashSet<String>();
objects.add("JAVA");
objects.add("JAVA");
System.out.println(objects.size());
}
第一步:程序首先创建一个Object泛型的Set数组,这里用到了上转型;
第二步:执行object里面的add添加方法,传进的值为“JAVA”;
首先HashSet源代码中会有HashSet的无参构造方法:说明只要创建一个HashSet数组就会默认创建一个HashMap数组代码如下:
public HashSet() {
map = new HashMap<>();
}
因为Set的add方法被HashSet重写过,所以运行时add会运行HashSet的add方法,此时我们打开HashSet中add方法进入hashset的add方法:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
此时参数e为刚刚传进的”JAVA“。
可以看到add方法调用的map里面的put方法,传入e和一个常量(常量全大写),判断是不是null(空)。
此时我们进入map里的put方法:
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
此时可以看到put需要的参数为key值和value对应刚刚传入的e和一个常量,此时可以看到put里面调用的是putVal方法,
传入的值为hash(key),key,value,false,true。此时打开参数列表中hash(key)方法来查看hash(key)的值到底是个什么样的值:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
hash方法传进来的参数为key为e也就是”JAVA“,这里为三目运算,若为空返回0,不为空返回一个”JAVA“特定的hashCode然后在被处理的值。这里的值是每个不同元素都是不同的值,不会重复。
此时搞明白参数列表我们就需要查看putVal方法体:
第一次添加成功发生在下边第六行代码
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
// 参数hash,key,value,onlyIfAbsent,evict其中hash随你key参数变化而变化,key为你要存入的数据(”JAVA“),后三位为常量 false true;
Node<K,V>[] tab; Node<K,V> p; int n, i;//第一步:创建tab,p数组和int类型的n和i:进入判断语句
if ((tab = table) == null || (n = tab.length) == 0)//将table地址值赋值给tab ,table为一个全局变量默认值为null,而且后边判断条件也成立,直接进入if方法体:
n = (tab = resize()).length;//此时需要进入resize方法,在上边快捷进入resize()方法块。
if ((p = tab[i = (n - 1) & hash]) == null)//因为第一次添加,tab的第(n - 1) & hash位为null进入,此时i=(n - 1) & hash
tab[i] = newNode(hash, key, value, null);//看源码可知newNode为数组,操作tab,因为tab内存地址和table相同,所以相当于改变了table 第一次在此处添加
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;//最后返回给put方法null
}
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;//此时全局变量table为null赋给oldTab
int oldCap = (oldTab == null) ? 0 : oldTab.length;//三目运算 得到oldCap为0
int oldThr = threshold;//threshold为全局变量第一次调用值为0,此时oldThr为0
int newCap, newThr = 0;//这两个值也为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被赋值了一个常量为16,newThr作为扩容被赋值为常量具体为16*0.75
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;//将newThr值赋值给threshold
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];//现在newCap为16意思就是现在newTab是容量为16的数组
table = newTab;//将newTab内存地址赋给table
if (oldTab != null) {//因为此时为第一次,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数组长度为16,然后继续运行putVal()方法点击下方跳转回putVal方法
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//返回给add null值
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;//返回true给add,在方法内已经添加,返回值为ture
}
Set<String> objects = new HashSet<String>();
objects.add("JAVA");//ture添加成功
objects.add("JAVA");//此时走第二个add添加
最后进入putVal()方法体;此时hash已经经过跟上一个一样的计算过程此处不再赘述,可知此时hesh和上一次hash相等
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)//先将tab = table(table内存地址给tab)不为空,长度不为0,不进入
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)//此时tab第i位置元素为"JAVA",不为空不进入
tab[i] = newNode(hash, key, value, null);
else { //进入此语句体
Node<K,V> e; K k;
if (p.hash == hash && //判断hash相等不相等通过
((k = p.key) == key || (key != null && key.equals(k))))//p.key为上一次的"JAVA"与这次"JAVA"相等通过
//key.equals(k)比较内容是否相等调用String的equals
e = p; //把p地址给e,走完判断语句
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 //判断e不为空么,成立进入
V oldValue = e.value; //e.value值赋给 oldValue
if (!onlyIfAbsent || oldValue == null) //onlyIfAbsent为true前面传参提到不再赘述
e.value = value; //都是常量没变化
afterNodeAccess(e);
return oldValue; //返回一个和上一次没区别的数组给put
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//得到putVal返回给的oldValue在返回给HashSet的add方法
}
public boolean add(E e) {
return map.put(e, PRESENT)==null; //因为oldValue数组不为空返回false没添加成功。
}
至此添加add结束,正好印证set集合不能重复的规律。