Methods in HashSet

METHOD

DESCRIPTION

add(E e) Used to add the specified element if it is not present, if it is present then return false.
clear() Used to remove all the elements from the set.
contains(Object o) Used to return true if an element is present in a set.
remove(Object o)                                                            Used to remove the element if it is present in set.
iterator()  Used to return an iterator over the element in the set.
isEmpty() Used to check whether the set is empty or not. Returns true for empty and false for a non-empty condition for set.
size() Used to return the size of the set.
clone()                                                    Used to create a shallow copy of the set.

HashSet in Java

Java HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the hash sets which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class also offers constant time performance for the basic operations like add, remove, contains, and size assuming the hash function disperses the elements properly among the buckets, which we shall see further in the article.  

Similar Reads

Java HashSet Features

A few important features of HashSet are mentioned below:...

Internal Working of a HashSet

...

Constructors of HashSet class

All the classes of the Set interface are internally backed up by Map. HashSet uses HashMap for storing its object internally. You must be wondering that to enter a value in HashMap we need a key-value pair, but in HashSet, we are passing only one value....

Methods in HashSet

To create a HashSet, we need to create an object of the HashSet class. The HashSet class consists of various constructors that allow the possible creation of the HashSet. The following are the constructors available in this class....

Performing Various Operations on HashSet

...

Performance of HashSet

METHOD DESCRIPTION add(E e) Used to add the specified element if it is not present, if it is present then return false. clear() Used to remove all the elements from the set. contains(Object o) Used to return true if an element is present in a set. remove(Object o)                                                            Used to remove the element if it is present in set. iterator()  Used to return an iterator over the element in the set. isEmpty() Used to check whether the set is empty or not. Returns true for empty and false for a non-empty condition for set. size() Used to return the size of the set. clone()                                                    Used to create a shallow copy of the set....

Methods Used with HashSet

Let’s see how to perform a few frequently used operations on the HashSet....

FAQs in HashSet in Java

...