Java HashSet Class

The HashSet class implements the Set interface and extends AbstractSet, uses HashTable mechanism to store the elements. 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.

Constructors

HashSet Class supports 4 types of constructors

HashSet()

Creates a new empty set which has HashMap as the backing instance with default initial capacity (16) and load factor (0.75).

HashSet(Collection<? extends E> c)

Creates a Set containing all the elements of the specified collection, in the order they are returned by the collection’s iterator.

HashSet(int initialCapacity)

Creates a new empty Set which has HashMap as the backing instance with the specified initial capacity and load factor (0.75).

HashSet(int initialCapacity, int capacityIncrement)

Creates a new empty Set which has HashMap as the backing instance with the specified initial capacity and load factor.

Methods of Vector Class

add(E e)
Adds the specified element to this set if it is not already present.

clear()
Removes all of the elements from this set.

clone()
Returns a shallow copy of this HashSet instance, the elements themselves are not cloned.

contains(Object o)
Returns true if this set contains the specified element.

isEmpty()
Returns true if this set contains no elements.

iterator()
Returns an iterator over the elements in this set.

remove(Object o)
Removes the specified element from this set if it is present.

size()
Returns the number of elements in this set.