HashSet

 HashSet

一、概述

  1. 版本:jdk 1.8.0_131

  2. HashSet 的底层实现通过 HashMap 完成,添加的内容作为 HashMap的 KeySet ,value 为固定值

  3. 线程不安全

  4. Key 允许 null 值

  5. 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;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值