package com.hspedu.hashset_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class Course521 {
public static void main(String[] args) {
// HashSet源码1、2
/*
* debugger源码分析:
* 1、new HashMap();
* 给定默认的加载因子 this.loadFactor = DEFAULT_LOAD_FACTOR; // 0.75
* 2、add方法 => map.put => putVal(key, value) key是需要存放的元素,value是static值
* 其中hash(key)会根据需要保存的集合元素key计算获得一个hash值,集合元素存放的位置i = (n - 1) & hash,再判断i的位置是否为null
* 最终返回null则表示添加成功,返回是查询到的对象,表示已经存在该对象,添加失败
*
* 添加相同元素的情况:
* (1)定义一个临时的节点p指向该位置链表的第一个元素对象,p.hash和添加的元素传入putVal方法的hash比较,
* 并且满足key(添加的元素对象)和指向的node节点的key是同一个对象,或者key不为null时,通过equals方法比较内容相同时,不添加对象
* (2)判断p位置是否是红黑树结构,调用putTreeVal比较判断
* (3)不满足1、2则为链表的形式,for循环遍历比较,e = p.next,即直接和第二个元素对象进行比较,p = e使得p指向的位置依次下移比较...
* 如果把元素添加到最后null的位置,会判断索引值,如果索引>=7,即满足8个元素对象时,加入第九个元素时创建树结构(创建树时如果数组tab的长度不满足64先扩容数组)
* */
HashSet hashSet = new HashSet();
hashSet.add("java");
hashSet.add("php");
hashSet.add("java");
System.out.println("hashSet = " + hashSet);
/*
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; // newCap = DEFAULT_INITIAL_CAPACITY; 初始化给定默认值16
if ((p = tab[i = (n - 1) & hash]) == null) // 判断tab[i]位置上是否已经存在元素
tab[i] = newNode(hash, key, value, null); // 如果为null则新增节点对象
else { // 如果为非null则判断
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k)))) // 比较改节点位置元素的hash key
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) // 判断size是否达到临界的阈值capacity * 0.75,例如初始化16*0.75=12
resize();
afterNodeInsertion(evict);
return null;
}
*/
}
}
package com.hspedu.hashset_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class Course523 {
public static void main(String[] args) {
// HashSet源码3
/*
* 1、HashSet第一次添加元素,底层tab数组扩容到16,临界值是16*0.75=12(0.75是底层的final加载因子)
* 2、数组到临界值12时,就会扩容到16*2=32,临界值32*0.75=24以此类推
* 3、如果数组一个位置的链表到达8时,并且tab数组容量到64时,则创建树结构储存
* 4、如果数组一个位置的链表到达8时,但是tab数组容量没有到64,仍然是2倍的数组扩容先扩容数组tab大小
*
* (debugger过程可以看出hash计算的值就是hashCode的值)
*
* */
HashSet hashSet = new HashSet();
// for (int i = 1; i <= 100; i++) {
// hashSet.add(i);
// }
for (int i = 1; i <= 12; i++) {
hashSet.add(new A(i));
}
/*
// 如果数组上一条链表的长度达到8时,会调用treeifyBin树化方法
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) // MIN_TREEIFY_CAPACITY = 64; 数组的如果小于64则优先resize扩容
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);
}
}
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 && // 数组扩容2倍数,大小从初始16->32
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold // 数组临界扩容阈值,大小从初始12->24
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY; // 默认容量16->32
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); // 默认阈值12->24
}
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数组
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;
}
*/
}
}
class A {
private int n;
public A(int n) {
this.n = n;
}
// 重写hash使添加的元素在一条链表上
@Override
public int hashCode() {
return 100;
}
}