使用HashMap的HashSet


声明:本文转载自https://my.oschina.net/qixiaobo025/blog/1551591,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

插播一条广告→2021 ByteDance字节跳动内推←各城市、各方向的岗位都有,大量招人!


背景

由于HashMap的源码我们已经分析过了原来你是这样的HashMap(1.7)

那么基于已经有的容器我们要如何做HashSet的实现呢?

以下我们将简单描述一下实现流程

源码

类图

构造函数

/**  * 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<>(); }   /**  * 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<>(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<>(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<>(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  */ HashSet(int initialCapacity, float loadFactor, boolean dummy) {     map = new LinkedHashMap<>(initialCapacity, loadFactor); }

那么思路很明确了,就是直接使用HashMap作为容器。

那么HashSet放入HashMap的value直接用一个默认对象即可。

// Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();

搞定收工!

这也是一种常见的委托模式(只暴露自己想要暴露的接口)

本文发表于2017年10月17日 16:34
(c)注:本文转载自https://my.oschina.net/qixiaobo025/blog/1551591,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1259 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1