java HashSet源码简单分析
·
测试代码
@SuppressWarnings({"all"})
public class HashSetSource {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("java");
hashSet.add("php");
hashSet.add("java");
System.out.println("set = " + hashSet);
}
}
源码分析
- 执行 HashSet()
public HashSet() {
map = new HashMap<>();
}
- 进入add(),底层是HashMap
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
- 调用put()方法,
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
- 该方法还会执行hash(key), 得到key对应的hash值算法
h = key.hashCode()) ^ (h >>> 16)
- 调用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;
}
- 首先定义了辅助变量
Node<K,V>[] tab; Node<K,V> p; int n, i;
tab 是HashMap的 一个数组,类型是Node
- if语句,如果tab为null或者tab的长度为0。那么就要进行第一次扩容, 为16个空间
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
- 根据key,得到hash 去计算该key应该存放到table表的哪个索引位置,并把这个位置的对象赋给p
- 判断p是否为
null
2.1 如果为null,表示还没存放元素,就创建一个Node,这个Node就是我们添加的第一个元素Node(key = “java”, value=PRESENT);
然后放在tab[i]位置
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
- 这里在需要局部变量的时候,直接在对应代码块创建了
else {
Node<K,V> e; K k;
....
}
-
- 如果准备加入的key 和 p指向的Node结点是同一个对象
-
- 如果p指向的Node结点的 key 的equals() 和准备加入的key比较后相同
- 满足以上任意一个则不能将结点加入
- 判断p是不是一颗红黑树,如果是就调用putTreeVal,来进行添加
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
-
- 依次和该链表的每一个元素比较后, 都不相同,则加入到该链表的最后
- 注意在把元素添加到链表后,立即判断 该链表是否已经达到8个结点
- 如果已经到达8个,就调用
treeifyBin(tab, hash)对当前链表进行树化(红黑树)
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY(64))
resize();
- 如果上面条件成立,先 table 扩容. 只有上面条件不成立时,才进行转成红黑树
- 如果比较过程中有相同情况,就直接break
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;
}
- 每添加成功一个结点,size就要++
...
...
...
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;
...
...
...
更多推荐



所有评论(0)