How to create Hashset program in java

import java.util.*;

class TestCollection9 
{
    public static void main(String args[]) 
    {
        // Creating HashSet and adding elements
        HashSet<String> set = new HashSet<String>();
        set.add("Rajendra");
        set.add("Raja");
        set.add("Ravi");
        set.add("TechSarvam");

        // Traversing elements using Iterator
        Iterator<String> itr = set.iterator();
        while (itr.hasNext()) 
        {
            System.out.println(itr.next());
        }
    }
}

Step-by-Step Explanation:

1. Importing Required Package:

import java.util.*;

  • This line imports the java.util package, which contains the HashSet and Iterator classes. 
  • These classes are needed to create a HashSet and iterate over it.

2. Class Declaration:

class TestCollection9 {

For the class TestCollection9, it has a main() method. This is how Java programs start.

3. Main Method:

public static void main(String args[]) {

This is the main() method where the execution of the program begins. Inside this method, the logic for the program is written.

4. Declaring a HashSet:

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

A HashSet is defined as set to hold String elements. A Java collection class, HashSet, will not permit any duplicate elements. It will keep elements in an unordered manner.

5. Adding Elements to the HashSet:

set.add(“Rajendra”);

set.add(“Raja”);

set.add(“Ravi”);

set.add(“TechSarvam”);

  • The add() method is employed to add elements into the HashSet. Here, the strings “Rajendra”, “Raja”, “Ravi”, and “TechSarvam” are added to the set.

Important Note: HashSet does not permit duplicate elements, so if the same string was added again it would not get inserted in the set.

6. Creating an Iterator:

Iterator<String> itr = set.iterator();

An Iterator is created for the HashSet using the iterator() method. This iterator will allow us to traverse through the elements of the HashSet.

7. Traversing the HashSet using the Iterator:

while (itr.hasNext()) {

       System.out.println(itr.next());

   }

  • The while loop checks if there are more elements in the HashSet using itr.hasNext().
  • If hasNext() returns true the loop enters the body, and itr.next() is invoked to fetch the next element in the set.
  • The fetched element is then printed onto the console using System.out.println().

8. Output of the Program:

The program will print the elements in the set. Since HashSet does not maintain the order of insertion, the elements will be printed in an arbitrary order. The elements will not appear in the order in which they were added (which was “Rajendra”, “Raja”, “Ravi”, “TechSarvam”).

Possible Output:

   Rajendra

   Ravi

   Raja

TechSarvam

 or any other order, as HashSet does not guarantee insertion order.

Key Points:

1. HashSet:

It is a collection class that implements the Set interface. It stores unique elements and does not maintain the order of insertion. It uses hashing to store the elements, making it very efficient for operations like searching, insertion, and deletion.

2. Iterator:

The Iterator is used to traverse through the collection (in this case, HashSet). It provides methods like hasNext() (checks if there is a next element) and next() (returns the next element).

3. Unordered Nature of HashSet:

The HashSet does not preserve the order of elements, so the order in which elements are retrieved (via the iterator) may not be the same as the order in which they were added.

4. No Duplicates Allowed in HashSet:

If you try to add duplicate elements to a HashSet, the set will not allow them. For example, if you were to call add(“Rajendra”); again, then the set would still only contain one occurrence of “Rajendra”.

Adding Duplicate Elements Example

set.add(“Rajendra”);

set.add(“Rajendra”);  // This will not be added again.

Full Output Example (Unordered)

Rajendra

Ravi

Raja

TechSarvam

In a nutshell, this program shows how one might generate a HashSet, add elements to it, and then use an Iterator to traverse and print out its contents. Its traversal order isn’t guaranteed because HashSet does not preserve the insertion order.

Leave a Comment

Your email address will not be published. Required fields are marked *