Java HashSet class is used to store unique elements. It uses hash table internally to store the elements. It implements Set interface and extends the AbstractSet class. Declaration of the is given below.
public class HashSet<E>extends AbstractSet<E>implements Set<E>, Cloneable, Serializable
HashSet class has three constructors that can be used to create HashSet accordingly.
HashSet() //This creates an empty HashSet
HashSet( Collection C ) //This creates a HashSet that is initialized with the elements of the Collection C
HashSet( int capacity ) //This creates a HashSet that has the specified initial capacity
In this example, we are creating a HashSet which is initially empty. Later on we will add items to this collection.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Displaying HashSet
System.out.println(hs);
}
}
[]
Method | Description |
---|---|
add(E e) | It adds the specified element to this set if it is not already present. |
clear() | It removes all of the elements from the set. |
clone() | It returns a shallow copy of this HashSet instance: the elements themselves are not cloned. |
contains(Object o) | It returns true if this set contains the specified element. |
isEmpty() | It returns true if this set contains no elements. |
iterator() | It returns an iterator over the elements in this set. |
remove(Object o) | It removes the specified element from this set if it is present. |
size() | It returns the number of elements in the set. |
spliterator() | It creates a late-binding and fail-fast Spliterator over the elements in the set. |
In this example, we are creating a HashSet that store string values. Since HashSet does not store duplicate elements, we tried to add a duplicate elements but the output contains only unique elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Displaying HashSet
System.out.println(hs);
}
}
[Mohan, Sohan, Rohan]
To remove elements from the hashset, we are using remove() method that remove the specified elements.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Displaying HashSet
System.out.println(hs);
// Removing elements
hs.remove("Mohan");
System.out.println("After removing elements: \n"+hs);
}
}
[Mohan, Sohan, Rohan] After removing elements: [Sohan, Rohan]
Since HashSet is a collection then we can use loop to iterate its elements. In this example we are traversing elements using for loop. See the below example.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Traversing ArrayList
for(String element : hs) {
System.out.println(element);
}
}
}
Mohan Sohan Rohan
Sometimes we want to know number of elements an HashSet holds. In that case we use size() then returns size of HashSet which is equal to number of elements present in the list.
import java.util.*;
class Demo
{
public static void main(String args[])
{
// Creating HashSet
HashSet<String> hs = new HashSet<String>();
// Adding elements
hs.add("Mohan");
hs.add("Rohan");
hs.add("Sohan");
hs.add("Mohan");
// Traversing ArrayList
for(String element : hs) {
System.out.println(element);
}
System.out.println("Total elements : "+hs.size());
}
}
Mohan Sohan Rohan Total elements : 3