HashSet Class in Java with Example

The HashSet class in Java is the first implementation class of Set interface. It is used to creates a collection that uses a hash table for storage. A hash table stores the information by using a mechanism called hashing. In the hashing technique, the informational content of key is used to determine a unique value, called its hash code. The hash code is used as the index at which the data associated with the key is stored.
HashSet class in java with example


Important Points about Java HashSet Class:

1. The underlying data structure for the hash set is the hash table.
2. In HashSet, duplicates are not allowed.
3. Insertion order is not preserved, elements are stored on the basis of their hash code.
4. HashSet allows you to add heterogeneous elements.
5. In HashSet, null insertion is possible.
6. It implements Serializable and Clonable interface but doesn't implement RandomAccess interface.
7. HashSet is the best for search operations.

Constructors in Java HashSet Class:

There are 4 constructors are available in Java HashSet class, which are described below:

1. HashSet(): This constructor creates a default hash set.

2. HashSet(int capacity): It is used to initialize the capacity of the hash set to the given capacity.

3. HashSet(int capacity, float loadFactor): This constructor is used to initialize the capacity of the hash table to the given capacity and the given load factor.

4. HashSet(Collection<? extends E> c): It is used to initializes the hash set by using the elements of collection c.

Methods of Java HashSet Class:


1. boolean add(Element e): This method is used to add the element in the hash set.

2. void clear(): This method is used to remove all the elements from the hash set.

3. boolean contains(Element e): This method is used to check whether the specified element present in the hash set or not. If the element found in the hash set then it returns true otherwise it returns false.

4. boolean isEmpty(): This method is used to check the hash set is empty or not. If it is empty it returns true else returns false.

5. int size(): This method returns the number of elements from the hash set.

6. boolean remove(Element e): This method is used to remove the specified element from the hash set.

7. Object clone(): This method is used to returns a shallow copy of the hash set.


Java HashSet Example to add elements:



import java.util.*; 
class hashSetExample
{
public static void main(String args[])
{
//creating a hash set
HashSet<String> hset = new HashSet<String>();

//adding elements in hash set
hset.add("Amit");
hset.add("Sumit");
hset.add("Rohit");
hset.add("Virat");
hset.add("Vijay");

//adding duplicate elements in hash set
hset.add("Amit");
hset.add("Virat");

//adding null value in hash set
hset.add(null);
hset.add(null);

//displaying hash set elements
System.out.println("Elements in the hash set are: " +hset);
}
}


Output:

Elements in the hash set are: [null, Rohit, Vijay, Amit, Sumit, Virat]


Java HashSet Example to remove elements:


import java.util.*;
class hashSetExample
{
public static void main(String args[])
{
//creating a hash set
HashSet<String> hset = new HashSet<String>();

//adding elements in hash set
hset.add("Amit");
hset.add("Sumit");
hset.add("Rohit");
hset.add("Virat");
hset.add("Vijay");
hset.add(null);
System.out.println("Hash Set elements are: " +hset);

//removing a specific element from hash set
hset.remove("Amit");
System.out.println("After removing(Amit) HashSet element is: " +hset);

//removing all elements from hash set
hset.clear();

//displaying hash set elements
System.out.println("Elements in the hash set are: " +hset);
}

Output:

Hash Set elements are: [null, Rohit, Vijay, Amit, Sumit, Virat]
After removing(Amit) Hash Set element is: [null, Rohit, Vijay, Sumit, Virat]
Elements in the hash set are: []



Java HashSet Example to find the number of elements in the hash set:


import java.util.*;

class hashSetExample

{

public static void main(String args[])

{

//creating a hash set

HashSet<String> hset = new HashSet<String>();



//adding elements in hash set

hset.add("Amit");

hset.add("Sumit");

hset.add("Rohit");

hset.add("Virat");

hset.add("Vijay");



//find the number of elements in the hash set

int elements = hset.size();

System.out.println("The size of the hash set is: " +elements);

}

}


Output: 
The size of the hash set is: 5


Java HashSet example to convert a hash set into a List/ArrayList:


import java.util.*;
class hashSetExample 
{ 
public static void main(String args[]) 
{
//creating a hash set 
HashSet<String> hset = new HashSet<String>();

//adding elements in hash set 
hset.add("Amit"); 
hset.add("Sumit"); 
hset.add("Rohit"); 
hset.add("Virat"); 
hset.add("Vijay"); 
System.out.println("Hash Set element is: " +hset);

//converting a hash set into an array list. 
List<String> alist = new ArrayList<String>(hset); 
System.out.println("ArrayList is: " +alist); 
} 
}

Output:

Hash Set elements are: [Rohit, Vijay, Amit, Sumit, Virat]
ArrayList is:  [Rohit, Vijay, Amit, Sumit, Virat]



You may also like:

What is Collection framework in Java?
ArrayList in Java with Example
LinkedList class in Java with Example
Vector in Java with Example
Stack implementation in Java
Exception handling in java- complete tutorial
A complete guide about inheritance in java with example
JSP Standard Tag Library- JSTL Tutorial
Session management techniques in servlet
Types of JDBC Driver in Java


HashSet Class in Java with Example HashSet Class in Java with Example Reviewed by Prashant Srivastava on March 03, 2019 Rating: 5

No comments:

Powered by Blogger.