java基础(十一)HashSet

HashSet扩展AbstractSet并且实现Set接口。它创建一个类集,该类集使用散列表进行存储。散列表通过使用称之为散列法的机制来存储信息。在散列(hashing)中,一个关键字的信息内容被用来确定唯一的一个值,称为散列码(hash code)。而散列码被用来当做与关键字相连的数据的存储下标。关键字到其散列码的转换是自动执行的——你看不到散列码本身。你的程序代码也不能直接索引散列表。散列法的优点在于即使对于大的集合,它允许一些基本操作如add( ),contains( ),remove( )和size( )方法的运行时间保持不变

 

散列表又称为哈希表。

散列表算法的基本思想是:

以结点的关键字为自变量,通过一定的函数关系(散列函 数)计算出对应的函数值,以这个值作为该结点存储在散 列表中的地址。

• 当散列表中的元素存放太满,就必须进行再散列,将产生 一个新的散列表,所有元素存放到新的散列表中,原先的 散列表将被删除。在Java语言中,通过负载因子(load factor)来决定何时对散列表进行再散列。例如:如果负 载因子是0.75,当散列表中已经有75%的位置已经放满, 那么将进行再散列。

• 负载因子越高(越接近1.0),内存的使用效率越高,元素 的寻找时间越长。负载因子越低(越接近0.0),元素的寻 找时间越短,内存浪费越多。

• HashSet类的缺省负载因子是0.75。

 

下面的构造函数定义为:

– HashSet( )

– HashSet(Collection c)

– HashSet(int capacity)

– HashSet(int capacity, float fillRatio)

 

第一种形式构造一个默认的散列集合。

第二种形式用c中的元素初始化散列集合。

第三种形式用capacity初始化散列集合的容量。

第四种形式用它的参数初始化散列集合的容量和填充比(也称为加载容量)。填充比必须介于0.0与1.0之间,它决定在散列集合向上调整大小之前,有多少能被充满。具体的说,就是当元素的个数大于散列集合容量乘以它的填充比时,散列集合被扩大。对于没有获得填充比的构造函数,默认使用0.75

 

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

 

可以从构造函数中清楚的看到,HashSet底层是通过构建Map来实现的。默认情况下是构建HashMap。

那么,在存放过程中,又是怎么存放的呐?

  /**
     * 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
     */
    public boolean add(E e) {
	      return map.put(e, PRESENT)==null;
    }

 

可以看到,HashSet将传入的值作为底层Map的key,所有的key都有一样的value,即PRESENT.

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

 当迭代HashSet时,底层操作如下:

/**
     * Returns an iterator over the elements in this set.  The elements
     * are returned in no particular order.
     *
     * @return an Iterator over the elements in this set
     * @see ConcurrentModificationException
     */
    public Iterator<E> iterator() {
	  return map.keySet().iterator();
    }

 

HashSet的其他操作,都是在操作底层的map,只要明白HashMap的操作就可以了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值