关于HashCode
通过HashCode方法,我们可以得到一个对象的地址
public class Test{
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.hashCode()); //得到并输出test的地址
System.out.println(Integer.toHexString(test.hashCode())); //得到并输出test的十六进制地址
System.out.println(test); //默认调用toString方法
System.out.println(test.toString()); //toString方法中调用hashCode方法
}
}
主函数:
import java.util.HashSet;
public class Test{
public static void main(String[] args) {
HashSet<String> set = new HashSet<>(); //创建HashSet对象,调用了该类中无参构造方法,执行了该构造方法中map = new HashMap<>();map为HashSet全局变量
set.add("Tom"); //调用HashSet add方法,map.put(e, PRESENT)中map为全局变量,map指向的是创建HashSet对象时创建的HashMap对象
}
}
调用的方法:
//Set中的add方法
public boolean add(E e) {//传入E类型的参数e
return map.put(e, PRESENT)==null;//调用put方法,并传入e和一个常量
}
//Map中的put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//调用putVal方法和Hash方法,传入hash值,key值,value值和,false,true。
}
//hash方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//如果key不为null,把key的地址赋值到h中,并与该地址无符号右移十六位后进行异或运算,并返回值
}
//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;//定义一个Node<K,V>类型的数组tab,定义Node<K,V>的变量p
if ((tab = table) == null || (n = tab.length) == 0)//①
n = (tab = resize()).length;//②
if ((p = tab[i = (n - 1) & hash]) == null)//把通过n和hash运算得到的值赋给i,把tab[i]赋值给p,判断p是否为空
tab[i] = newNode(hash, key, value, null);//p为空,存入第一次输入的数据
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;//返回null
}
①if ((tab = table) == null || (n = tab.length) == 0):table为一个全局变量,初始值为null。先判断逻辑或的第一个表达式:将table值赋值给tab,table为null,故tab为null,此时逻辑或后第二个表达式将被短路(短路效应)。此时if语句判断为true。
②n = (tab = resize()).length;这里调用resize方法,resize方法中把newTab赋值给table,所以table的地址等于newTab,然后返回newTab,这时通过tab = resize()将newTab赋值给tab,所以tab地址也等于newTab,所以此时,tab和table指向同一个地方。此时,n即为数组长度:16
//resize方法: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;//进行类型容量定义:长度16
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];//在这里创建了一个newTab数组,且为newCap类型进行强制类型转换,但长度仍为16
table = newTab;//将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返回
}
//主函数
import java.util.HashSet;
public class Test{
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Tom");
set.add("Tom");//尝试添加一个相同的元素
}
}
//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)//存入第一个数之后,全局变量table为第一次的newTab,赋值给tab不为null,第一个false;经过第一个式子的赋值,tab.length为16不为零;故if中都为false
n = (tab = resize()).length;//
if ((p = tab[i = (n - 1) & hash]) == null)//由于n与第一次相同,key值相同,hash值也相同,故p仍为存储着第一个的位置,不为空,false;
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))))//hash值根据key值我们知道是相同的,而k与key因为字符串在常量池中为同一地址(定义方式相同,字符串相同),故为true
e = p;//将p赋值给e,故e现在存储的也为同key值的那个已经存储过的元素(在本例中是第一个元素)
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不为null,true
V oldValue = e.value;//赋值语句
if (!onlyIfAbsent || oldValue == null)//onlyIfAbsent为调用putVal输入的参数,为false,所以!后,为true,进入if的true分支
e.value = value;//新的value值,覆盖原有的value值
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
//主函数
import java.util.HashSet;
public class Test{
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Tom");
set.add(new String("Tom"));//new语句创建新对象,值相同
}
}
//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)//存入第一个数之后,全局变量table为第一次的newTab,赋值给tab不为null,第一个false;经过第一个式子的赋值,tab.length为16不为零;故if中都为false
n = (tab = resize()).length;//
if ((p = tab[i = (n - 1) & hash]) == null)//由于n与第一次相同,key值相同,hash值也相同(String类中,重写了HashCode方法),故p仍为存储着第一个的位置,不为空,false;
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;//将p赋值给e,故e现在存储的也为同key值的那个已经存储过的元素(在本例中是第一个元素)
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不为null,true
V oldValue = e.value;//赋值语句
if (!onlyIfAbsent || oldValue == null)//onlyIfAbsent为调用putVal输入的参数,为false,所以!后,为true,进入if的true分支
e.value = value;//新的value值,覆盖原有的value值
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
③((k = p.key) == key || (key != null && key.equals(k)))):hash值根据key值我们知道是相同的;而k与key因为不同对象,所以地址不同false;key不为null,且为String类型,调用String类中重写的equals方法,比较结果,k的字符串与key的字符串相同(key值相同),进入if的true分支