HashSet是外部STL中的类,用于向收集器中存储或快速检索数据。收集器中的这些数据唯一,且数值充当关键字。
添加元素:
hashset.add(E e):返回boolean型,如果此 set 中尚未包含指定元素,则添加指定元素;如果此 set 已包含该元素,则该调用不更改 set 并返回 false。删除元素:
hashset.clear():从此 set 中移除所有元素。
hashset.remove(Object o):如果指定元素存在于此 set 中,则将其移除。
hashset.isEmpty():如果此 set 不包含任何元素,则返回 true。
hashset.contains(Object o):如果此 set 包含指定元素,则返回 true。
hashset.size():返回此 set 中的元素的数量(set 的容量)。
Constructor and Description |
---|
HashSet()
Constructs a new, empty set; the backing
HashMap instance has default initial capacity (16) and load factor (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).
|
HashSet(int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing
HashMap instance has the specified initial capacity and the specified load factor.
|
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.
|