本文的目录结构:
一、HashSet 的 Javadoc 文档注释和简要说明
截个图,然后来观摩 HashSet 的javadoc,照样是几点总结摘抄:
Set 接口的实现类,内部使用了一个 HashMap 实例;不保证 set 的迭代顺序(无序);允许存储 null
通常情况下(假如 hash 分布比较均匀),基本操作(add, remove, contains 和 size)可以看成是 O(1) 的;迭代/遍历的时间和 HashSet 的元素数量以及内部 HashMap 实例的的内部数组大小有关,因此对于迭代遍历性能有要求的, HashSet 的初始容量不能设置太大或者负载因子不能太小。
HashSet 也是非线程安全的,需要其他的工具类来保证线程安全
HashSet 也是 fail-fast;同样也并不保证出现有并发修改就百分百抛出 ConcurrentModificationException
二、HashSet 的内部实现:内部属性和构造函数
这个就比较简洁了,一个 HashMap 实例,用于存储元素 e,也即是 k,另外就是一个共用的 Object 对象,其实就是内部 HasMap 实例的 value。
//内部使用的 HashMap 实例
private transient HashMapmap;//Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
再来看一看构造函数,都似曾相识,其实基本就是 HashMap 的构造函数调用一遍,还有一个包级私有的构造函数,内部创建了 LinkedHashMap,这个是给 LinkedHashSet 用的,双链表保证有序。
/*** Constructs a new, empty set; the backing HashMap instance has
* default initial capacity (16) and load factor (0.75).
* 默认初始容量 16 和 负载因子 0.75*/
publicHashSet() {
map= new HashMap<>();
}/*** Constructs a new set containing the elements in the specified
* collection. The HashMap is created with default load factor
* (0.75) and an initial capacity sufficient to contain the elements in
* the specified collection.
*
*@paramc the collection whose elements are to be placed into this set
*@throwsNullPointerException 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 HashMap instance has
* the specified initial capacity and the specified load factor.
* 指定 初始容量 和 负载因子
*@paraminitialCapacity the initial capacity of the hash map
*@paramloadFactor the load factor of the hash map
*@throwsIllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive*/
public HashSet(int initialCapacity, floatloadFactor) {
map= new HashMap<>(initialCapacity, loadFactor);
}/*** Constructs a new, empty set; the backing HashMap instance has
* the specified initial capacity and default load factor (0.75).
* 初定 初始容量,默认负载因子 0.75
*@paraminitialCapacity the initial capacity of the hash table
*@throwsIllegalArgumentException if the initial capacity is less
* than zero*/
public HashSet(intinitialCapacity) {
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.
* 给 LinkedHashSet 用的构造函数,其实和 HashMap 套路类似,只是 LinkedHashMap 有序
*@paraminitialCapacity the initial capacity of the hash map
*@paramloadFactor the load factor of the hash map
*@paramdummy ignored (distinguishes this
* constructor from other int, float constructor.)
*@throwsIllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive*/HashSet(int initialCapacity, float loadFactor, booleandummy) {
map= new LinkedHashMap<>(initialCapacity, loadFactor);
}
三、HashSet 的 add 操作和扩容
也比较简单直接,就是调用内部 HashMap 实例的 put 方法。当然扩容也是 HashMap 本身的扩容。
这里还是摘抄下 javadoc,大致就是说 set 中不会有重复的 key,e 和 e2 重复的判断条件是 (e==null ? e2==null : e.equals(e2))。 e 存在的时候该方法返回 false。
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
/*** Adds the specified element to this set if it is not already present.
* More formally, adds the specified element e to this set if
* this set contains no element e2 such that
* (e==null ? e2==null : e.equals(e2)).
* If this set already contains the element, the call leaves the set
* unchanged and returns false.
*
*@parame element to be added to this set
*@returntrue if this set did not already contain the specified
* element*/
public booleanadd(E e) {return map.put(e, PRESENT)==null;
四、HashSet 的 remove 操作
remove 移除元素,内部判断存在的条件是 o==null ? e==null : o.equals(e)
/*** Removes the specified element from this set if it is present.
* More formally, removes an element e such that
* (o==null ? e==null : o.equals(e)),
* if this set contains such an element. Returns true if
* this set contained the element (or equivalently, if this set
* changed as a result of the call). (This set will not contain the
* element once the call returns.)
*
*@paramo object to be removed from this set, if present
*@returntrue if the set contained the specified element*/
public booleanremove(Object o) {return map.remove(o)==PRESENT;
}