主程序:
import java.util.HashSet;
public class Test2 {
public static void main(String[] args) {
HashSet<String> names = new HashSet<String>();
names.add("Jim");
names.add("Jim");//这篇讲解这行如何执行
}
}
HashSet中的add方法:
在add方法的底层代码中,又调用了put方法
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
HashMap中的put方法:
在put中又调用了hash方法和putVal方法
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//因为存的第一个对象和第二个要存的对象的哈希值相同,所以第二次存的对象与第一次存的对象hash(key)结果是一样的
}
hash方法:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);//key不为零时,返回key的哈希值
}
HashMap中的putVal方法:
又调用了resize方法
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全局变量已经有值了,所以table,也就是tab不为空,而且tab.length不等于0,数组的长度为16,所以该if为false,所以这个if语句块不执行
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)//因为第一个和第二个值的hashCode相同,所以存第一个值和存第二个值的时候下标的值是一样的,因为第一次已经在此(n - 1) & hash 位置存了数据,所以tab[i = (n - 1) & hash]不为null,也就是p不为null,所以该if为false,所以这个if语句块不执行,将执行else语句块 结论:向HaspMap存储数据时,如果当前数据的hashcode和集合中已存在的元素的hashcode相同,则该if不成立
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))))//p.hash为第一个Jim的hash值,所以p.hash == hash 成立,由于两次的对象时一个地址,所以(k = p.key) == key也成立,【当是names.add(new String("Jim"));时 则(p.hash == hash &&((k = p.key) == key)为false;而(key != null && key.equals(k)) 为true】
e = p;//将第一个Jim的地址赋给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
V oldValue = e.value;// e.value 常量Object
if (!onlyIfAbsent || oldValue == null)// 在put方法中传给putVal方法的onlyIfAbsent本来就为false,所以!onlyIfAbsent true
e.value = value;
afterNodeAccess(e);
return oldValue;//返回第一个Jim,存储失败
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
}
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;
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;//将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;//resize方法返回newTab
}