Java面试基础篇——第十三篇:HashSet的实现

原创
2018/07/27 15:40
阅读数 168

原谅我不怎么会瞎BB,瞎扯淡这种事不大会,,,只会贴源码,为了照顾英文不大好的同学,我稍微简单的翻译一下,水平有限,大家多多包容。

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    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).
	 * 创建一个新的空的set, 默认大小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.
     *  构造一个包含指定collection元素的新set,默认的扩容因子是0.75,指定
     * collection中所有元素的初始容量来创建一个HashMap。
     * @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.
     * 以指定的initialCapacity和loadFactor构造一个空的HashSet。
     * @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).
     * 以指定的initialCapacity构造一个空的HashSet。
     * @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.
     * 以指定的initialCapacity和loadFactor构造一个新的空链接哈希集合。
     * 此构造函数为包访问权限,不对外公开,实际只是是对LinkedHashSet的支持。
     * @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);
    }

  • HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持。
  • 它不保证set 的迭代顺序;特别是它不保证该顺序恒久不变,此类允许使用null元素。
  • 在HashSet中,元素都存到HashMap键值对的Key上面,而Value则有一个统一的值private static final Object PRESENT = new Object();
  • 当有新值加入时,底层的HashMap会判断Key值是否存在。如果不存在,则插入新值,同时这个插入的细节会依照HashMap插入细节;如果存在就不插入。
  • HashSet很多操作数据的方法实质上都是调用HashMap的方法。
  • 对于HashSet中保存的对象,要注意正确重写其equals和hashCode方法,以保证放入的对象的唯一性。
展开阅读全文
打赏
2
3 收藏
分享
加载中
更多评论
打赏
0 评论
3 收藏
2
分享
返回顶部
顶部