HashSet
一、总结(jdk 1.8.0_131)
1.HashSet 的底层实现通过 HashMap 完成,添加的内容作为 HashMap的 KeySet ,value 为固定值
2.线程不安全
3.允许 null 值
4.clone 是浅拷贝
二、类
public class HashSet extends AbstractSet implements Set, Cloneable, Serializable {
三、构造方法
// 成员属性:全局的 map private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map // 定义默认的 final Object 作为 value private static final Object PRESENT = new Object(); /** * 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<E,Object>(); } /** * 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<E,Object>(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<E,Object>(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<E,Object>(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 */ // protected 访问权限,仅限在包内访问,不对外提供此接口 HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor); }
三、常用方法
// 返回 集合中 key 的数量 public int size() { return map.size(); } // 是否包含某个值;即 map 中是否包含某个 key public boolean contains(Object o) { return map.containsKey(o); } // 添加元素 // 若此时的key已经存在,则返回已有的value值,添加失败,返回false // 反之,返回 null,添加成功 public boolean add(E e) { return map.put(e, PRESENT)==null; } // 删除 key 时,map 回返回当前 key 对应的 value 值 public boolean remove(Object o) { return map.remove(o)==PRESENT; }