一、hash方法
在进行add之前要知道hash方法对各种数据类型的操作规则
public class Test1 {
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
public static void main(String[] args) {
//字符串类型对象
int hash = hash("tom");
System.out.println(hash);
hash = hash(new String("tom"));
System.out.println(hash);
//基本类型包装类对象
hash = hash(10000);
System.out.println(hash);
hash = hash(new Integer(10000));
System.out.println(hash);
//自定义类型对象
Student stu = new Student();
hash = hash(stu);
System.out.println(hash);
hash = hash(new Student());
System.out.println(hash);
}
}
上述代码执行结果为:
可见若为String类型,所传内容相同,返回值相同。若为基本类型包装类,所传内容即为返回值。若为自定义类,所传地址不同,返回值内容不同。
二、add()方法执行过程
有下面代码:
public class Test {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("tom");
set.add("tom");
set.add("lucy");
}
}
add方法的跳转过程是:HashSet中的add方法→HashMap中的put方法→HashMap中的putVal,本文章主要分析在set集合中增加String类型对象时putVal方法中的执行过程。
下面代码为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)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
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;
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;
}
1、执行第五行代码set.add(“tom”);的执行过程
- 第一步执行if ((tab = table) == null || (n = tab.length) == 0)
第一次add(“tom”)时table数组为空此if语句成立,执行if语句内部程序:tab = resize()给tab一个数组空间,其中table为全局变量,tab和table指向同一个数组。 - 第二步执行if ((p = tab[i = (n - 1) & hash]) == null)
tab[i = (n - 1) & hash] 是根据所传key的hash值对tab数组空间的一个映射,也就是(n - 1) & hash为数组下标。此时tab[i]中没有数据,if语句成立,执行tab[i] = newNode(hash, key, value, null);新建一个Node对象存到tab中。table与tab指向同一个数组,且table为全局变量,故下次判断table是否为null时可以得到结果false。 - 第三步跳过else语句执行下列代码
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
2、执行第六行代码set.add(“tom”);的执行过程
- 第一步执行if ((tab = table) == null || (n = tab.length) == 0)
此时tab与table指向同一个数组,table为全局变量,且table中已经有元素,此次if中语句未成立,跳过。 - 第二步执行if ((p = tab[i = (n - 1) & hash]) == null)
hash=hash(“tom”),if语句中(n - 1) & hash所得数组下标与第一次add(“tom”)所添加对象的下标一致,故不为null语句不成立,其中tab[i]赋值给p。 - 跳转到else语句
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
p.hash即为tab[i].hash代表hash(“tom”),而hash=hash(“tom”)故p.hash == hash为true,同理p.key == key也成立,此if语句成立执行 e = p;
- 跳转到下列代码
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
e不为null,返回原值。
3、执行第七行代码set.add(“lucy”);的执行过程
- 第一步执行if ((tab = table) == null || (n = tab.length) == 0)
此时tab与table指向同一个数组,table为全局变量,且table中已经有元素,此次if中语句未成立,跳过。 - 第二步执行if ((p = tab[i = (n - 1) & hash]) == null)
(n - 1) & hash计算下标,hash=hash(“lucy”),tab[i]为null,if语句成立,执行tab[i] = newNode(hash, key, value, null);新建一个Node对象存到tab中。 - 第三步跳过else语句执行下列代码
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
三、table数组
ctrl+点击resize(),阅读resize()方法发现有下列代码
newCap = DEFAULT_INITIAL_CAPACITY;
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
再ctrl+点击DEFAULT_INITIAL_CAPACITY常量发现
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
DEFAULT_INITIAL_CAPACITY=16,即table长度为16,所存类型为<K,V>