📜  Java.util.HashSet类

📅  最后修改于: 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 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 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).

方法继承

此类从以下类继承方法-

  • java.util.AbstractSet
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.Set