目录
All Implemented Interfaces(所有已实现接口)
Direct Known Subclasses(直接已知子类)
所在包:java.util.HashSet<E>
- Type Parameters(参数类型):
E
- the type of elements maintained by this set由这个集合维护的元素的类型
- All Implemented Interfaces(所有已实现接口):
- Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>
- Direct Known Subclasses(直接已知子类):
- JobStateReasons,LinkedHashSet
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, Serializable
This class implements the Set interface, backed by a hash table (actually a HashMap instance).
It makes no guarantees as to the iteration order of the set;
in particular, it does not guarantee that the order will remain constant over time.
This class permits the null element.
这个类实现Set接口,由一个散列表(实际上是一个HashMap实例)支持。
它不保证集合的迭代顺序;
特别是,它不能保证顺序在一段时间内保持不变。
该类允许空元素。
This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.
Iterating over this set requires time proportional to the sum of the HashSet instance's size (the number of elements) plus the "capacity" of the backing HashMap instance (the number of buckets).
Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.
这个类为基本操作(添加、删除、包含和大小)提供了常数时间性能,假设散列函数正确地将元素分散到各个bucket中。
遍历这个集合需要的时间与HashSet实例的大小(元素的数量)和支持HashMap实例的“容量”(桶的数量)的总和成比例。
因此,如果迭代性能很重要,那么不要将初始容量设置得太高(或负载因子太低)是非常重要的。
Note that this implementation is not synchronized.
If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
This is typically accomplished by synchronizing on some object that naturally encapsulates the set.
If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet
method.
This is best done at creation time, to prevent accidental unsynchronized access to the set:
注意,这个实现不是同步的。
如果多个线程同时访问一个散列集,并且至少有一个线程修改该散列集,则必须在外部对其进行同步。
这通常是通过对一些自然封装了集合的对象进行同步来实现的。如果不存在这样的对象,则应该使用集合来“包装”集合。
synchronizedSet方法。
这最好在创建时完成,以防止意外的不同步访问集:
Set s = Collections.synchronizedSet(new HashSet(...));
The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the Iterator throws a ConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
这个类的迭代器方法返回的迭代器是快速失效的:如果在迭代器创建后的任何时候修改集合,除了通过迭代器自己的删除方法之外,任何方式都可以,迭代器抛出ConcurrentModificationException。因此,在面对并发修改时,迭代器会快速而干净地失败,而不是在将来某个不确定的时间冒任意的、不确定的行为的风险。
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification.Fail-fast iterators throw ConcurrentModificationException on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
注意,不能保证迭代器的快速故障行为,因为通常来说,在存在非同步并发修改的情况下,不可能做出任何严格的保证。故障快速迭代器在最大努力的基础上抛出ConcurrentModificationException。因此,编写一个依赖于这个异常的正确性的程序是错误的:迭代器的快速故障行为应该只用于检测bug。
This class is a member of the Java Collections Framework.
该类是Java集合框架的成员。
- Since:
- 1.2
- See Also:
Collection
,Set
,TreeSet
,HashMap
, Serialized Form
Constructor Summary(构造函数的总结)
Constructor and Description 构造函数和描述 |
---|
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
构造一个新的空集;支持的HashMap实例具有默认的初始容量(16)和负载因子(0.75)。 |
HashSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
构造一个新集合,该集合包含指定集合中的元素。
|
HashSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
构造一个新的空集;支持的HashMap实例具有指定的初始容量和缺省负载因子(0.75)。
|
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
构造一个新的空集;支持的HashMap实例具有指定的初始容量和指定的负载因子。
|
Method Summary(方法总结)
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Adds the specified element to this set if it is not already present.
|
void |
clear()
Removes all of the elements from this set.
|
Object |
clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
|
boolean |
contains(Object o)
Returns true if this set contains the specified element.
|
boolean |
isEmpty()
Returns true if this set contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
boolean |
remove(Object o)
Removes the specified element from this set if it is present.
|
int |
size()
Returns the number of elements in this set (its cardinality).
|
Spliterator<E> |
spliterator()
Creates a late-binding and fail-fast
Spliterator over the elements in this set. |
Methods inherited from class java.util.AbstractSet
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.lang.Iterable
Constructor Detail(构造函数详述)
-
HashSet
public HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
构造一个新的空集;支持的HashMap实例具有默认的初始容量(16)和负载因子(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.
构造一个新集合,该集合包含指定集合中的元素。
HashMap是使用默认的负载因子(0.75)和足够容纳指定集合中的元素的初始容量创建的。- 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.
构造一个新的空集;支持的HashMap实例具有指定的初始容量和指定的负载因子。
- 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).
构造一个新的空集;支持的HashMap实例具有指定的初始容量和缺省负载因子(0.75)。- Parameters:
initialCapacity
- the initial capacity of the hash table哈希表的初始容量- Throws:
IllegalArgumentException
- if the initial capacity is less than zero 如果初始容量小于零
Method Detail(方法详述)
-
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.
在这个集合的元素上返回一个迭代器。元素没有特定的顺序返回。- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceSet<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an Iterator over the elements in this set
在这个集合的元素上返回一个迭代器 - See Also:
ConcurrentModificationException
-
size
public int size()
Returns the number of elements in this set (its cardinality).
返回此集合中的元素数(其基数)。- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this set (its cardinality)
这个集合中元素的数量(它的基数)
-
isEmpty
public boolean isEmpty()
Returns true if this set contains no elements.
如果该集合不包含任何元素,则返回true。- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
- true if this set contains no elements
如果这个集合不包含元素,返回true
-
contains
public boolean contains(Object o)
Returns true if this set contains the specified element.
More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
如果该集合包含指定的元素,则返回true。
更正式地说,当且仅当这个集合包含一个元素e (o==null ?e = = null: o.equals (e))。 -
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- element whose presence in this set is to be tested
要测试其在此集合中的存在的元素- Returns:
- true if this set contains the specified element
如果这个集合包含指定的元素,则为真
-
add
public boolean add(E e)
Adds the specified element to this set if it is not already present.
More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)).
If this set already contains the element, the call leaves the set unchanged and returns false.
如果指定的元素尚未出现,则将其添加到此集合。
更正式地说,如果这个集合不包含e2元素,则将指定的元素e添加到这个集合中,使得(e==null ?e2 = = null: e.equals (e2))。
如果该集合已经包含元素,则调用将保持集合不变并返回false。- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
e
- element to be added to this set
元素被添加到这个集合中
- Returns:
- true if this set did not already contain the specified element
如果该集合尚未包含指定的元素,则为true
-
remove
public boolean remove(Object o)
Removes the specified element from this set if it is present.
More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if this set contains such an element.
Returns true if this set contained the element (or equivalently, if this set changed as a result of the call).
(This set will not contain the element once the call returns.)
如果指定的元素存在,则从该集合中移除它。
更正式地说,删除一个元素e (o==null ?
如果这个集合包含这样一个元素,那么e==null: o.equals(e))。
如果该集合包含元素,则返回true(或者,如果该集合由于调用而发生更改,则返回true)。
(一旦调用返回,这个集合将不包含元素。)- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
o
- object to be removed from this set, if present
对象,如果存在,则从该集合中移除- Returns:
- true if the set contained the specified element
如果集合包含指定的元素,则为true
-
clear
public void clear()
Removes all of the elements from this set. The set will be empty after this call returns.
从这个集合中移除所有的元素。在这个调用返回后,这个集合将为空。- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
clone
public Object clone()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
返回此HashSet实例的浅拷贝:元素本身没有被克隆。
-
spliterator
public Spliterator<E> spliterator()
Creates a late-binding and fail-fastSpliterator
over the elements in this set.
The
Spliterator
reportsSpliterator.SIZED
andSpliterator.DISTINCT
.
Overriding implementations should document the reporting of additional characteristic values.
在这个集合的元素上创建一个后期绑定和故障快速Spliterator。
Spliterator传达给Spliterator.SIZED和Spliterator.DISTINCT。
覆盖实现应该记录额外特征值的报告。- Specified by:
spliterator
in interfaceIterable<E>
- Specified by:
spliterator
in interfaceCollection<E>
- Specified by:
spliterator
in interfaceSet<E>
- Returns:
- a
Spliterator
over the elements in this set
在这个集合的元素上返回一个Spliterator - Since:
- 1.8