1 Star 0 Fork 382

大哥 / source-code-hunter

forked from doocs / source-code-hunter 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
HashSet.md 3.14 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2020-09-03 17:04 . fix: 更新文档格式

HashSet 本身并没有什么特别的东西,它提供的所有集合核心功能,都是基于 HashMap 来实现的。如果了解 HashMap 源码的实现,HashSet 源码看起来跟玩一样。我的博客中有专门分析 HashMap 源码的文章,不熟悉的请自行翻阅。

HashSet 的特点如下:

  • 内部使用 HashMap 的 key 存储元素,以此来保证元素不重复
  • HashSet 是无序的,因为 HashMap 的 key 是无序的;
  • HashSet 中允许有一个 null 元素,因为 HashMap 允许 key 为 null;
  • HashSet 是非线程安全的。
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable {
    static final long serialVersionUID = -5024744406713321676L;

    // 基于HashMap实现
    private transient HashMap<E,Object> map;

    // 只需要用到HashMap中key唯一的特性,所以value全部使用同一个 Object实例填充,节省内存空间
    private static final Object PRESENT = new Object();

    /**
     * 实例化 HashSet 的时候,初始化内部的 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);
    }

    /**
     * 根据初始容量和扩容因子实例化 HashSet,减少rehash频率,提升性能,原理与HashMap相同
     */
    public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }

    /**
     * 同上
     */
    public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }

    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }

    /**
     * 返回迭代器,用于迭代
     * 下面所有的功能都是基于 HashMap 来实现的
     */
    public Iterator<E> iterator() {
        return map.keySet().iterator();
    }

    /**
     * 元素个数
     */
    public int size() {
        return map.size();
    }

    /**
     * 是否为空
     */
    public boolean isEmpty() {
        return map.isEmpty();
    }

    /**
     * 是否包含给定元素
     */
    public boolean contains(Object o) {
        return map.containsKey(o);
    }

    /**
     * 添加元素,如果 Set集合中未包含该元素,返回true
     */
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

    /**
     * 删除元素,如果Set集合包含该元素,返回true
     */
    public boolean remove(Object o) {
        return map.remove(o)==PRESENT;
    }

    /**
     * 清除元素
     */
    public void clear() {
        map.clear();
    }

    /**
     * 浅克隆
     */
    @SuppressWarnings("unchecked")
    public Object clone() {
        try {
            HashSet<E> newSet = (HashSet<E>) super.clone();
            newSet.map = (HashMap<E, Object>) map.clone();
            return newSet;
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e);
        }
    }
}
1
https://gitee.com/dagechen/source-code-hunter.git
git@gitee.com:dagechen/source-code-hunter.git
dagechen
source-code-hunter
source-code-hunter
main

搜索帮助