HashSet是基于HashMap来实现的,因为它的底层是通过HashMap来保存元素的。
1.属性
// 用于存储存放到HashSet中的元素
private transient HashMap<E,Object> map;
// HashSet中存放的是一个元素,而HashMap存放的是key-value键值对,此PRESENT对象就是value值 // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();
2.构造器
/** * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has * default initial capacity (16) and load factor (0.75).
* 构造一个空的HashSet,HashMap默认的初始容量是16,加载因子是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
* 构造一个包含指定集合元素的Set */ 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
* 构造一个空的HashSet,底层HashMap是根据指定的参数初始化容量和加载因子来构造的 */ public HashSet(int initialCapacity, float loadFactor) { map = new HashMap<>(initialCapacity, loadFactor); }
3.方法
3.1 boolean add(E e):如果此 set 中尚未包含指定元素,则添加指定元素
/** * Adds the specified element to this set if it is not already present. * More formally, adds the specified element <tt>e</tt> to this set if * this set contains no element <tt>e2</tt> such that * <tt>(e==null ? e2==null : e.equals(e2))</tt>. * If this set already contains the element, the call leaves the set * unchanged and returns <tt>false</tt>. * * @param e element to be added to this set * @return <tt>true</tt> if this set did not already contain the specified * element
* 添加指定元素到set中,如果set中已经包含了该元素,那么不会重复添加,并且返回false;否则,返回true */ public boolean add(E e) { return map.put(e, PRESENT)==null; }
3.2 boolean remove(Object o):如果指定元素存在于此 set 中,则将其移除。
/** * Removes the specified element from this set if it is present. * More formally, removes an element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>, * if this set contains such an element. Returns <tt>true</tt> 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.) * * @param o object to be removed from this set, if present * @return <tt>true</tt> if the set contained the specified element
* 如果此set包含指定的元素,那么移除然后返回true */ public boolean remove(Object o) { return map.remove(o)==PRESENT; }
3.3 boolean contains(Object o):如果此 set 包含指定元素,则返回 true。
/** * Returns <tt>true</tt> if this set contains the specified element. * More formally, returns <tt>true</tt> if and only if this set * contains an element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this set is to be tested * @return <tt>true</tt> if this set contains the specified element */ public boolean contains(Object o) { return map.containsKey(o); }
总结:HashSet其实比较简单,它不包含重复的元素,它的底层就是HashMap,HashSet中的元素就是HashMap中的key,HashMap中的value值则是一个静态的Object对象。