HashSet is a class in the Java collections framework that implements the Set interface. It is a high-performance set implementation that stores its elements in a hash table, which allows for fast insertion, deletion, and search operations.

One of the main advantages of using a HashSet is that it does not allow duplicate elements. When you add an element to a HashSet, it will first check if the element already exists in the set. If it does, the element will not be added. If it does not, the element will be added to the set. This makes HashSet a good choice for storing unique elements.

Here are some common operations you can perform with a HashSet in Java:

  1. Adding an element: To add an element to a HashSet, you can use the add() method. 
  2.     For example:
    HashSet<String> set = new HashSet<>();
    set.add("apple");
    set.add("banana");
    set.add("cherry");



  1. Removing an element: To remove an element from a HashSet, you can use the remove() method.
    •  For example:
    • set.remove("banana");


  1. Checking if an element exists: To check if an element exists in a HashSet, you can use the contains() method. 
    • For example:
    • if (set.contains("apple")) {
          System.out.println("Apple exists in the set.");
        }
  1. Iterating over the elements: To iterate over the elements in a HashSet, you can use the for-each loop. 
    • For example:
    • for (String fruit : set) {
          System.out.println(fruit);
        }


  1. Clearing the set: To remove all elements from a HashSet, you can use the clear() method. 
    • For example:
    •   set.clear();


In conclusion, HashSet is a useful class in the Java collections framework that allows you to store and manipulate a set of unique elements efficiently. It provides fast insertion, deletion, and search operations and does not allow duplicate elements. By using the various methods provided by the HashSet class, you can perform a variety of operations on a set of elements in your Java program.


Write a java program to perform the following HashSet operations:

i. Iterate through HashSet.
ii. Copy Set content to another HashSet.
iii. Delete all elements from HashSet.
iv. Copy all elements from HashSet to an array.
v. Compare two sets and retain elements which are same on both sets.
vi. Eliminate duplicate user defined objects from HashSet.
vii. Find user defined objects from HashSet.
viii. Delete user defined objects from HashSet.

java code:


import java.util.*;
class HashSetDemo {
    public static void main(String[] args)
    {
       HashSet<String> h = new HashSet<String>();
 
        h.add("RedmiNote10");
        h.add("Honor9");
        h.add("Realme8");
 
        // Adding duplicate elements
        h.add("Vivo");
 
        // Displaying the HashSet
        System.out.println(h);
        System.out.println("List contains RedmiNote10 or not:"+ h.contains("RedmiNote10"));
 
        h.remove("Honor9");
        System.out.println("List after removing Honor9:"+ h);

        System.out.println("Iterating over list:");
 
        // Iterating over hashSet items
        Iterator<String> i = h.iterator();
        while (i.hasNext())
             // Iterating over elements
             System.out.println(i.next());

        Object[] arr = h.toArray();
        System.out.println("The array is:");
        for (int j = 0; j < arr.length; j++)
            System.out.println(arr[j]);

        HashSet<String> copySet = new HashSet<>(h);
        System.out.println("Original HashSet: " + h);
        System.out.println("Copy HashSet: " + copySet);

        HashSet<String> h1 = new HashSet<String>();
 
        h1.add("RedmiNote10");
        h1.add("Iphone13");
        h1.add("Samsung");
        h.retainAll(h1);
        System.out.println("HashSet content:");
        System.out.println(h);
   
  }
}



output :
[Realme8, Honor9, Vivo, RedmiNote10]
List contains RedmiNote10 or not:true
List after removing Honor9:[Realme8, Vivo, RedmiNote10]
Iterating over list:
Realme8
Vivo
RedmiNote10
The array is:
Realme8
Vivo
RedmiNote10
Original HashSet: [Realme8, Vivo, RedmiNote10]
Copy HashSet: [Realme8, Vivo, RedmiNote10]
HashSet content:
[RedmiNote10]