集合框架--HashSet

72 阅读3分钟

1. 简介

HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合;允许有 null 值;是无序的,不会记录插入的顺序;不是线程安全的,如果多个线程尝试同时修改 HashSet,则最终结果是不确定的,必须在多线程访问时显示同步对 HashSet 的开发访问;实现了 Set 接口

image-20210201221025144

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{}
  • 底层使用 HashMap 来存储元素
  • 不保证集合中存放元素的顺序
  • 允许有且仅有一个 null 值
  • 线程不安全
  • 集合中的元素不重复

2. 成员变量

// Dummy value to associate with an Object in the backing Map
// HashSet 使用了 HashMap 的key,定义一个静态的常量来充当 value
private static final Object PRESENT = new Object();
// 底层使用的 HashMap 来实现的
private transient HashMap<E,Object> map;

3. 构造方法

// 无参构造方法,创建一个 HashMap 对象
public HashSet() {
    map = new HashMap<>();
}
// 根据集合创建 HashSet
public HashSet(Collection<? extends E> c) {
    map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
    addAll(c);
}
// 根据给定大小和加载因子创建
public HashSet(int initialCapacity, float loadFactor) {
    map = new HashMap<>(initialCapacity, loadFactor);
}
// 根据给定大小和默认加载因子创建
public HashSet(int initialCapacity) {
    map = new HashMap<>(initialCapacity);
}
// 该方法不对外公开的,构造的是一个 LinkedHashMap,是提供给 LinkedHashSet 使用的,dummy 无意义
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
    map = new LinkedHashMap<>(initialCapacity, loadFactor);
}

4. 方法

/**
 * Returns the number of elements in this set (its cardinality).
 * 返回 set 中的元素的个数
 * @return the number of elements in this set (its cardinality)
 */
public int size() {
    return map.size();
}
/**
 * Returns <tt>true</tt> if this set contains no elements.
 * 判断集合是否为空
 * @return <tt>true</tt> if this set contains no elements
 */
public boolean isEmpty() {
    return map.isEmpty();
}
/**
 * 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&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
 * 查看集合中是否包含某个值,判断 HashMap 中是否包含该 key
 * @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);
}
/**
 * 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&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;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
 */
// 添加元素,这里的添加元素就是将要添加的元素作为key存储到HashMap中,value 为上面定义的一个常量
// 所以 HashMap 中存储的 key 不同但是 value 都是同一个值
public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}
/**
 * Removes the specified element from this set if it is present.
 * More formally, removes an element <tt>e</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;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
 */
// 删除元素
public boolean remove(Object o) {
    return map.remove(o)==PRESENT;
}

5. 保证元素不重复

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

在 add 方法中可以看到,这里的添加元素就是将要添加的元素作为key存储到HashMap中,value 为上面定义的一个常量,所以 HashMap 中存储的 key 不同但是 value 都是同一个值

在 HashMap 的 add 方法中,当新放入的 key 与集合中的 key 相同(hash 值相同并且 equals(key) 也相同),新添加的对象的 value 会覆盖掉原有的 vlaue,但是 key 不会有任何变化,因此 如果向 HashSet 中添加一个以及存在的元素时,新添加的集合元素将不会被放入 HashMap 中,原来的元素也不会有任何改变,即满足了Set 中的元素不重复的特性。

参考文献:

juejin.cn/post/684490…

juejin.cn/post/684490…