📅  最后修改于: 2020-11-14 06:17:20             🧑  作者: Mango
java.util.HashSet类实现由哈希表支持的Set接口,以下是有关HashSet的要点-
此类不能保证集合的迭代顺序。特别是,它不能保证顺序会随着时间的推移保持恒定。
此类允许使用null元素。
以下是java.util.HashSet类的声明-
public class HashSet
extends AbstractSet
implements Set, Cloneable, Serializable
以下是java.util.HashSet类的参数-
E-这是此集合维护的元素的类型。
Sr.No. | Constructor & Description |
---|---|
1 |
HashSet() This constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75). |
2 |
HashSet(Collection extends E> c) This constructs a new set containing the elements in the specified collection. |
3 |
HashSet(int initialCapacity) This constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75). |
4 |
HashSet(int initialCapacity, float loadFactor) This constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor. |
Sr.No. | Method & Description |
---|---|
1 | boolean add(E e)
This method adds the specified element to this set if it is not already present. |
2 | void clear()
This method removes all of the elements from this set. |
3 | Object clone()
This method returns a shallow copy of this HashSet instance, the elements themselves are not cloned. |
4 | boolean contains(Object o)
This method returns true if this set contains the specified element. |
5 | boolean isEmpty()
This method returns true if this set contains no elements. |
6 | Iterator This method returns an iterator over the elements in this set. |
7 | boolean remove(Object o)
This method removes the specified element from this set if it is present. |
8 | int size()
This method returns returns the number of elements in this set(its cardinality). |
此类从以下类继承方法-