1、hash方法
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
当传入的对象key是一个null时,返回值为0。
当输入不为null时,分为以下多种情况:
- 当为Integer类型时
比如代码:
Integer a = 11;
Integer b = 11;
System.out.println(hash(a));
System.out.println(hash(b));
由于a和b都不为空,会执行代码h = key.hashCode()
所以会调用hashCode方法,又由于Integer重写了hashCode方法:方法体如下:
public static int hashCode(int value) {
return value;
}
就说明传入的值(value)是什么,hashCode就返回什么,这里传入的a和b都是11,所以执行结果也都是输出11。
- 当为String类型时
比如代码:
String str1 = "11111";
String str2 = new String("11111");
System.out.println(str1);
System.out.println(str2);
由于str1和str2都不为空,会执行代码h = key.hashCode()
所以也会调用hashCode方法,又由于String重写了hashCode方法:方法体如下:
public int hashCode() {
int h = hash;
if (h == 0 && !hashIsZero) {
h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
if (h == 0) {
hashIsZero = true;
} else {
hash = h;
}
}
return h;
}
这个方法什么意思呢,也就是说如果两个构造字符串的字符相同的话,返回的hashCode值就相同,进而返回的hash也相同,即使如果对象不同,因为底层重写了hashCode方法,所以返回的值也相同。
- 总结
同一个对象调用多次该方法,结果相同;不同对象,如果重写了hashCode方法,使得其返回值相同,则多次调用该方法,结果相同
2、HashSet add方法(一)
先看代码:
import java.util.HashSet;
public class Test {
public static void main(String[] args) {
HashSet<String> names = new HashSet<String>();
names.add("Jim");//向HashMap集合的key存值,HashMap value 是一个常量Object
}
}
当调用方法时,会创建HashMap集合对象:
public HashSet() {
map = new HashMap<>();
}
add方法
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
HashMap put方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
HashMap 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为null,tab也为null,所以这个if判断为true,所以会继续执行这个resize()方法。
n = (tab = resize()).length;//resize方法直接为table返回newTab,resize方法也返回newTab,所以tab和table是同一个对象
if ((p = tab[i = (n - 1) & hash]) == null)//tab指向一个数组,返回一个长度16的数组,i = (n - 1) & hash数组长度-1按位与hash值得到一个值i,i为下标,因为第一次执行,所以肯定null,if判断true。
tab[i] = newNode(hash, key, value, null);//i就是(n - 1) & hash,在这个位置添加一个元素,因为数组为地址传递,所以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;
}
resize方法
作用
- 创建一个数组,把这个数组赋给了全局变量table,又返回了这个数组
resize方法有一个返回值newTab,又有代码table = newTab;
所以table和newtable是同一个对象
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;
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];
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;
}