HashSet源码解析

compact1, compact2, compact3
java.util

Class HashSet<E>

此类实现Set接口,由哈希表(实际上是HashMap实例)支持。它不保证集合的迭代顺序。特别是,它不能保证顺序会随着时间的推移保持恒定。此类允许使用null 元素。

该类为基本操作(addremovecontainssize提供恒定的时间性能,假设hash函数将元素正确地分散在存储桶中。对此集合进行迭代需要的时间与HashSet实例的大小(元素的数量)加上后备HashMap实例的“容量” (存储桶的数量)之和成比例因此,如果迭代性能很重要,则不要将初始容量设置得过高(或负载因子过低),这一点非常重要。

请注意,此实现未同步。 如果多个线程同时访问哈希集,并且至少有一个线程修改了哈希集,则必须在外部对其进行同步。这通常是通过自然同步某些对象来完成的封装集合。如果不存在这样的对象,则应使用Collections.synchronizedSet 方法“包装”该集合 最好在创建时完成此操作,以防止意外地异步访问集合:

   设置s = Collections.synchronizedSet(new HashSet(...));

此类的迭代器方法返回的迭代器是 快速失败的:如果在创建迭代器后的任何时候以任何方式修改集合(除非通过迭代器自己的remove 方法),则迭代器将抛出ConcurrentModificationException因此,面对并发修改,迭代器将快速而干净地失败,而不是在未来的不确定时间内冒任意,不确定的行为的风险。

请注意,迭代器的快速失败行为不能为 保证的 通常来说,在存在不同步的并发修改的情况下,不可能做出任何严格的保证。快速失败的迭代器会尽最大努力抛出ConcurrentModificationException因此,编写依赖于此异常的程序的正确性是错误的:迭代器的快速失败行为应仅用于检测错误。

此类是 Java Collections Framework的成员 

Since:1.2See Also:CollectionSetTreeSetHashMapSerialized Form

构造器摘要

建设者
构造函数和描述
HashSet()
构造一个新的空集;支持的HashMap实例具有默认的初始容量(16)和负载因子(0.75)。
HashSet(Collection<? extends E> c)
构造一个新集合,其中包含指定集合中的元素。
HashSet(int initialCapacity)
构造一个新的空集;支持的HashMap实例具有指定的初始容量和默认负载因子(0.75)。
HashSet(int initialCapacity, float loadFactor)
构造一个新的空集;支持的HashMap实例具有指定的初始容量和指定的负载系数。

方法总结

所有方法实例方法具体方法
修饰符和类型方法和说明
boolean add(E e)
如果指定的元素尚不存在,则将其添加到该集合中。
void clear()
从该集合中删除所有元素。
Object clone()
返回此HashSet实例的浅表副本:元素本身未克隆。
boolean contains(Object o)
如果此集合包含指定的元素,则返回true
boolean isEmpty()
如果此集合不包含任何元素,则返回true
Iterator<E> iterator()
返回此集合中元素的迭代器。
boolean remove(Object o)
如果存在,则从此集合中删除指定的元素。
int size()
返回此集合中的元素数(其基数)。
Spliterator<E> spliterator()
在此集合中的元素上创建后绑定 和快速失败 Spliterator

Constructor Detail

  • HashSet

    public HashSet()
    Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).

  • HashSet

    public HashSet(Collection<? extends E> c)
    Constructs a new set containing the elements in the specified collection. The HashMap is created with default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection.
    Parameters:
    c - the collection whose elements are to be placed into this set
    Throws:
    NullPointerException - if the specified collection is null

  • HashSet

    public HashSet(int initialCapacity,
                   float loadFactor)
    Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
    Parameters:
    initialCapacity - the initial capacity of the hash map
    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

HashSet

public HashSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
Parameters:
initialCapacity - the initial capacity of the hash table
Throws:
IllegalArgumentException - if the initial capacity is less than zero

方法细节

  • size

    public int size()
     
    返回此集合中元素的数量(其 基数)。
    指定者:
    size 在界面中 Collection<E>
    指定者:
    size 在界面中 Set<E>
    指定者:
    size 在班上 AbstractCollection<E>
    返回值:
    此集合中元素的数量(其基数)

  • isEmpty

    public boolean isEmpty()
    如果此集合不包含任何元素,则返回true
    指定者:
    isEmpty 在界面中 Collection<E>
    指定者:
    isEmpty 在界面中 Set<E>
    覆写:
    isEmpty 在班上 AbstractCollection<E>
    返回值:
    如果此集合不包含任何元素,则为true

  • contains

    public boolean contains(Object o)
    如果此集合包含指定的元素,则返回true更正式地说,当且仅当此集合包含元素e使得 (o == null?e == null:o.equals(e))时,返回true
    指定者:
    contains 在界面中 Collection<E>
    指定者:
    contains 在界面中 Set<E>
    覆写:
    contains 在班上 AbstractCollection<E>
    参数:
    o -此集合中存在的元素将被测试
    返回值:
    如果此集合包含指定的元素,则为true

  • add

    public boolean add(E e)
    如果指定的元素尚不存在,则将其添加到该集合中。更正式地说,如果此集合不包含元素e2,则将指定的元素e添加到该集合中,从而使 (e == null?e2 == null:e.equals(e2))如果此集合已经包含该元素,则调用将使该集合保持不变,并返回false
    指定者:
    add 在界面中 Collection<E>
    指定者:
    add 在界面中 Set<E>
    覆写:
    add 在班上 AbstractCollection<E>
    参数:
    e -要添加到此集合中的元素
    返回值:
    如果此集合尚未包含指定的元素,则为true

  • remove

    public boolean remove(Object o)
    如果存在指定元素,则从此集合中删除该元素。更正式地说,移除元素Ë这样 (O == NULLé== NULL:o.equals(e)条),如果此set包含这样的元素。如果此集合包含元素(或等效,如果此集合由于调用而更改),则返回true(一旦调用返回,此集合将不包含该元素。)
    指定者:
    remove 在界面中 Collection<E>
    指定者:
    remove 在界面中 Set<E>
    覆写:
    remove 在班上 AbstractCollection<E>
    参数:
    o -从该集合中删除的对象(如果存在)
    返回值:
    如果集合包含指定的元素,则为true

  • clone

    public Object clone()
    返回此HashSet实例的浅表副本:元素本身未克隆。
    覆写:
    clone 在班上 Object
    返回值:
    此集的浅表副本
    也可以看看:
    Cloneable

原文地址:https://www.cnblogs.com/WLCYSYS/p/14227266.html