1.HashSet说明:
(1)元素不重复,可以添加null,只能有一个null(不重复)
(2)遍历HashSet的顺序和元素hash后的位置有关
(3)底层实现是HashMap
public HashSet() {
map = new HashMap<>();
}
2.HashSet底层源码分析:-> HashMap底层
public HashSet() {
map = new HashMap<>();
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
//private static final Object PRESENT = new Object();
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
//定义的辅助变量
Node<K,V>[] tab; Node<K,V> p; int n, i;
//table是hashmap的数组,类型是Node[]
//当前table是null或者大小=0,进行第一次扩容,大小16
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//根据key的哈希值定位到tab的位置,并将该位置对象赋给p,判断p是否为null
//1.如果该位置不存在p=null,直接放到该位置
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//2.如果已存在
else {
Node<K,V> e; K k;
//2.1如果和p位置是同一个对象或者equals比较后相同,则不加入
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//2.2判断p是不是一颗红黑树,如果是红黑树调用putTreeVal进行添加
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
//2.3如果是链表,遍历链表依次比较
else {
for (int binCount = 0; ; ++binCount) {
//都不相同,挂到链表末尾
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
//如果链表达到8个节点,进行树化(转成红黑树,条件tab.length>=MIN_TREEIFY_CAPACITY,否则先进行扩容)
//先扩容:if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY) resize();
if (binCount >= TREEIFY_THRESHOLD - 1) // TREEIFY_THRESHOLD = 8
treeifyBin(tab, hash);
break;
}
//比较过程中如果找到Node是同一个对象或者equals比较后相同,则不加入,直接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;
}
3.HashSet扩容和红黑树机制:
HashSet底层是HashMap,第一次添加时,table数组扩容到16,临界值(threshold)=16*0.75=12
当数组使用到临界值12就进行扩容16*2=32,新的临界值=32*0.75=24,以此类推
在java8中,如果一条链表的元素个数到达>=TREEIFY_THRESHOLD(默认8),如果tab.length<MIN_TREEIFY_CAPACITY(默认64)进行数组扩容,否则将链表转为红黑树
4.HashSet最近实践:Dog类,包含name、age,当name相同时认为是相同的,不能添加到HashSet中
public class Set_ {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("hello");
hashSet.add("hello");
//重写Dog类的equals方法,equals比较name是否一样
hashSet.add(new Dog("大黄",10));
hashSet.add(new Dog("大黄",20));
//String重写了equals方法,equals方法比较值
hashSet.add(new String("world"));
hashSet.add(new String("world"));
System.out.println(hashSet);
}
@Data
private static class Dog {
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Dog dog = (Dog) o;
return Objects.equals(name, dog.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
Java集合 HashSet源码
最新推荐文章于 2023-03-20 18:27:19 发布