Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎


Problem : Create a dynamic Set that contains only unique elements.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestHashSet.java  and copy below contents.

import java.util.*;

 * A program to implement HashSet.

public class TestHashSet {

    public static void main(String[] args) {

        HashSet hSet = new HashSet();

        // You can insert any object in the HashSet. Here it is string


        System.out.println("Some Importent Methods");
        System.out.println(" Set size : " + hSet.size());
        System.out.println(" Contains Jay : " + hSet.contains("Jay"));

         * Primitive data type need to be converted into Objects before
         * inserting. Get all elements and print with help of Iterator interface
        Iterator it = hSet.iterator(); // Get an iterator

        System.out.println("\nPrint All Elements with help of Iterator ");

        while (it.hasNext()) { // Checks if any element in list
            Object oo = it.next(); // Get next available element
            System.out.println(" From Iterator -- " + oo);

        // Clear all elements
        System.out.println(" Is Empty " + hSet.isEmpty());


Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestHashSet.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestHashSet.java with help of 
    • javac TestHashSet.java command. Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestHashSet command.


C:\sunilos>java TestHashSet
Some Importent Methods
 Set size : 3
 Contains Jay : true

Print All Elements with help of Iterator
 From Iterator -- Jay
 From Iterator -- Viru
 From Iterator -- Basanti
 Is Empty true


What is HashSet ?

This class implements the Set interface, backed by a hash table (actually a HashMap instance).  
This class permits the null element.
When you gets elements through Iterator, elements order may be changed.

Is HashSet thread-safe?

No it is not synchronized, if you want to make it thread safe then synchronize it with help of below command
    Set s = Collections.synchronizedSet(new HashSet());

Most Interesting methods in HashSet?

  • add() - 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() - 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() - Removes the specified element from this set if it is present.
  • size() -  Returns the number of elements in this set (its cardinality).

<<Previous | Next>>
Mahima Hardia,
Aug 19, 2011, 11:22 PM