Java HashSet

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java HashSet class

Java HashSet class hierarchy

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The important points about Java HashSet class are:

  • HashSet stores the elements by using a mechanism called hashing.

  • HashSet contains unique elements only.

Difference between List and Set

List can contain duplicate elements whereas Set contains unique elements only.

Hierarchy of HashSet class

The HashSet class extends AbstractSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order.

HashSet class declaration

Let's see the declaration for java.util.HashSet class.

  1. public class HashSet extends AbstractSet implements Set, Cloneable, Serializable  

Constructors of Java HashSet class:




It is used to construct a default HashSet.

HashSet(Collection c)

It is used to initialize the hash set by using the elements of the collection c.

HashSet(int capacity)

It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.

Methods of Java HashSet class:

Method Description
void clear() It is used to remove all of the elements from this set.
boolean contains(Object o) It is used to return true if this set contains the specified element.
boolean add(Object o) It is used to adds the specified element to this set if it is not already present.
boolean isEmpty() It is used to return true if this set contains no elements.
boolean remove(Object o) It is used to remove the specified element from this set if it is present.
Object clone() It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
Iterator iterator() It is used to return an iterator over the elements in this set.
int size() It is used to return the number of elements in this set.

Java HashSet Example

  1. import java.util.*;  
  2. class TestCollection9{  
  3.  public static void main(String args[]){  
  4.   //Creating HashSet and adding elements  
  5.   HashSet<String> set=new HashSet<String>();  
  6.   set.add("Ravi");  
  7.   set.add("Vijay");  
  8.   set.add("Ravi");  
  9.   set.add("Ajay");  
  10.   //Traversing elements  
  11.   Iterator<String> itr=set.iterator();  
  12.   while(itr.hasNext()){  
  13.    System.out.println(;  
  14.   }  
  15.  }  
  16. }  

Test it Now


Java HashSet Example: Book

Let's see a HashSet example where we are adding books to set and printing all the books.

  1. import java.util.*;  
  2. class Book {  
  3. int id;  
  4. String name,author,publisher;  
  5. int quantity;  
  6. public Book(int id, String name, String author, String publisher, int quantity) {  
  7. = id;  
  8. = name;  
  9. = author;  
  10.     this.publisher = publisher;  
  11.     this.quantity = quantity;  
  12. }  
  13. }  
  14. public class HashSetExample {  
  15. public static void main(String[] args) {  
  16.     HashSet<Book> set=new HashSet<Book>();  
  17.     //Creating Books  
  18.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  
  19.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  
  20.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  
  21.     //Adding Books to HashSet  
  22.     set.add(b1);  
  23.     set.add(b2);  
  24.     set.add(b3);  
  25.     //Traversing HashSet  
  26.     for(Book b:set){  
  27.     System.out.println(" "" "" "+b.publisher+" "+b.quantity);  
  28.     }  
  29. }  
  30. }  


101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6