E
-元素这一套保持型
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
这类的基本操作提供了稳定的性能(add,remove,contains和size),假设哈希函数的分散元素之间的正确的桶。在这集的迭代需要时间的HashSet实例的大小和比例(元素个数)加上“能力”的支持HashMap实例(桶的数量)。因此,它是非常重要的,不设置的初始容量太高(或负载因子太低),如果迭代性能是重要的。
请注意,此实现不同步。如果多个线程访问一个散列集的同时,并至少有一个线程修改的集合,它必须同步外部。这通常是通过同步的一些对象,自然封装设置完成。如果该对象不存在,设定应该是“包裹”使用Collections.synchronizedSet
方法。最好的做法是在创建时,防止意外的非同步访问的设置:
集的集合。synchronizedset(新好(…));
迭代器返回这个类的方法是iterator快速失败:如果集合在迭代器是创建的任何时间修改,以任何方式除了通过迭代器的remove方法,迭代器抛出一个ConcurrentModificationException
。因此,在遇到同步修改,迭代器不能快速、干净,而不是冒着任意,非在将来一个不确定的时间确定的行为。
注意迭代器不能快速失败行为得到保证的话,一般来说,不可能在不同步的并发修改的存在作出难以保证。快速失败迭代器扔ConcurrentModificationException尽最大努力的基础上。因此,要写一个程序,依靠这一例外的正确性错误:快速失败迭代器的行为只能用来检测错误。
这个班的一员 Java Collections Framework。
Collection
,
Set
,
TreeSet
,
HashMap
,
Serialized Form
Constructor and Description |
---|
HashSet()
构建了一个新的,空的设置;支持
HashMap实例具有默认初始容量(16)和负载因子(0.75)。
|
HashSet(Collection<? extends E> c)
构造一个包含指定集合中的元素的新集合。
|
HashSet(int initialCapacity)
构建了一个新的,空的设置;支持
HashMap实例具有指定的初始容量和加载因子(0.75)。
|
HashSet(int initialCapacity, float loadFactor)
构建了一个新的,空的设置;支持
HashMap实例的指定初始容量和负载因子。
|
Modifier and Type | Method and Description |
---|---|
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 在这个集合中的元素。
|
equals, hashCode, removeAll
addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
parallelStream, removeIf, stream
public HashSet()
public HashSet(Collection<? extends E> c)
c
的元素都被放置到这集的集合
NullPointerException
-如果指定集合为空
public HashSet(int initialCapacity, float loadFactor)
initialCapacity
-哈希映射的初始容量
loadFactor
-哈希Map加载因子
IllegalArgumentException
如果初始容量小于零,或者负载因子是负的
public HashSet(int initialCapacity)
initialCapacity
-哈希表的初始容量
IllegalArgumentException
如果初始容量小于零
public Iterator<E> iterator()
iterator
接口
Iterable<E>
iterator
接口
Collection<E>
iterator
接口
Set<E>
iterator
方法重写,继承类
AbstractCollection<E>
ConcurrentModificationException
public int size()
size
接口
Collection<E>
size
接口
Set<E>
size
方法重写,继承类
AbstractCollection<E>
public boolean isEmpty()
isEmpty
接口
Collection<E>
isEmpty
接口
Set<E>
isEmpty
方法重写,继承类
AbstractCollection<E>
public boolean contains(Object o)
contains
接口
Collection<E>
contains
接口
Set<E>
contains
方法重写,继承类
AbstractCollection<E>
o
-这个集合中存在的元素进行测试
public boolean add(E e)
add
接口
Collection<E>
add
接口
Set<E>
add
方法重写,继承类
AbstractCollection<E>
e
元素被添加到这个组
public boolean remove(Object o)
remove
接口
Collection<E>
remove
接口
Set<E>
remove
方法重写,继承类
AbstractCollection<E>
o
对象要从组中删除,如果存在
public void clear()
clear
接口
Collection<E>
clear
接口
Set<E>
clear
方法重写,继承类
AbstractCollection<E>
public Object clone()
public Spliterator<E> spliterator()
Spliterator
在这个集合中的元素。
报告的Spliterator
Spliterator.SIZED
和Spliterator.DISTINCT
。重写实现应记录附加特征值的报告。
spliterator
接口
Iterable<E>
spliterator
接口
Collection<E>
spliterator
接口
Set<E>
Spliterator
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.