前言
HashSet ,基于 HashMap 的 Set 实现类。在业务中,如果我们有排重的需求,一般会考虑使用 HashSet 。
hashset类图
实现 java.util.Set 接口,并继承 java.util.AbstractSet 抽像类。
实现 java.io.Serializable 接口。
实现 java.lang.Cloneable 接口
HashSet内部属性
HashSet内部属性就两个
private transient HashMap<E,Object> map;// Dummy value to associate with an Object in the backing Mapprivate static final Object PRESENT = new Object();
map 的 key ,存储 HashSet 的每个 key 。
map 的 value ,因为 HashSet 没有 value 的需要,所以使用一个统一的 PRESENT 即可。代码如下:
构造方法
/*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has* default initial capacity (16) and load factor (0.75).*/public HashSet() {map = new HashMap<>();}/*** Constructs a new set containing the elements in the specified* collection. The <tt>HashMap</tt> is created with default load factor* (0.75) and an initial capacity sufficient to contain the elements in* the specified collection.** @param c the collection whose elements are to be placed into this set* @throws NullPointerException if the specified collection is null*/public HashSet(Collection<? extends E> c) {map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));addAll(c);}/*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has* the specified initial capacity and the specified load factor.** @param initialCapacity the initial capacity of the hash map* @param loadFactor the load factor of the hash map* @throws IllegalArgumentException if the initial capacity is less* than zero, or if the load factor is nonpositive*/public HashSet(int initialCapacity, float loadFactor) {map = new HashMap<>(initialCapacity, loadFactor);}/*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has* the specified initial capacity and default load factor (0.75).** @param initialCapacity the initial capacity of the hash table* @throws IllegalArgumentException if the initial capacity is less* than zero*/public HashSet(int initialCapacity) {map = new HashMap<>(initialCapacity);}/*** Constructs a new, empty linked hash set. (This package private* constructor is only used by LinkedHashSet.) The backing* HashMap instance is a LinkedHashMap with the specified initial* capacity and the specified load factor.** @param initialCapacity the initial capacity of the hash map* @param loadFactor the load factor of the hash map* @param dummy ignored (distinguishes this* constructor from other int, float constructor.)* @throws IllegalArgumentException if the initial capacity is less* than zero, or if the load factor is nonpositive*/HashSet(int initialCapacity, float loadFactor, boolean dummy) {map = new LinkedHashMap<>(initialCapacity, loadFactor);}
在构造方法中,会创建 HashMap 或 LinkedHashMap 对象。
添加单个元素
#add(E e) 方法,添加单个元素。代码如下:
public boolean add(E e) {return map.put(e, PRESENT)==null;
}
map 的 value 值,就是我们看到的 PRESENT
private static final Object PRESENT = new Object();
而添加多个元素,继承自 AbstractCollection 抽象类,通过 #addAll(Collection<? extends E> c) 方法,代码如下:
public boolean addAll(Collection<? extends E> c) {boolean modified = false;// 遍历 c 集合,逐个添加for (E e : c)if (add(e))modified = true;return modified;
}
在方法内部,会逐个调用 #add(E e) 方法,逐个添加单个元素。
移除单个元素
#remove(Object key) 方法,移除 key 对应的 value ,并返回该 value 。代码如下:
public boolean remove(Object o) {return map.remove(o) == PRESENT;
}
查找单个元素
public boolean contains(Object o) {return map.containsKey(o);
}
清空
public void clear() {map.clear();
}
总结
HashSet 是基于 HashMap 的 Set 实现类。
hashmap源码分析:https://blog.csdn.net/qq_42600094/article/details/130556792