HashSet

A HashSet in Java is an implementation of the Set interface, which represents a collection of unique elements. It does not allow duplicate elements and does not guarantee the order of elements. The HashSet class uses a hash table for storage, which provides constant-time performance for basic operations like add, remove, contains, and size, assuming a good hash function distributes elements evenly across the table.

import java.util.HashSet;

// Syntax for creating a HashSet
HashSet<Type> setName = new HashSet<>();Code language: JavaScript (javascript)

Methods:

  1. Adding Elements
    • boolean add(element): Adds the specified element to the set if it is not already present.
  2. Removing Elements
    • boolean remove(element): Removes the specified element from the set if it is present.
    • void clear(): Removes all elements from the set.
  3. Checking Existence
    • boolean contains(element): Returns true if the set contains the specified element.
  4. Size and Empty Check
    • int size(): Returns the number of elements in the set.
    • boolean isEmpty(): Returns true if the set contains no elements.
  5. Iterating Over Elements
    • Iterator iterator(): Returns an iterator over the elements in the set.
    • forEach(Consumer action): Performs the given action for each element in the set.
public class HashSetExample {
    public static void main(String[] args) {
        // Creating a HashSet to store student names
        HashSet<String> studentSet = new HashSet<>();

        // Adding students to the HashSet
        studentSet.add("Paani");
        studentSet.add("Mahesh");
        studentSet.add("Datta");
        studentSet.add("Ganesh");
        studentSet.add("Harsha");

        // Adding a duplicate student (Mahesh)
        boolean addedDuplicate = studentSet.add("Mahesh");
        System.out.println("Duplicate 'Mahesh' added? " + addedDuplicate);

        // Displaying the set of students
        System.out.println("\nSet of students:");
        for (String student : studentSet) {
            System.out.println(student);
        }

        // Removing a student from the set
        studentSet.remove("Datta");

        // Displaying the updated set
        System.out.println("\nUpdated set of students:");
        for (String student : studentSet) {
            System.out.println(student);
        }

        // Checking if the set contains a student
        String searchStudent = "Harsha";
        boolean containsStudent = studentSet.contains(searchStudent);
        System.out.println("\nDoes the set contain '" + searchStudent + "'? " + containsStudent);

        // Size of the set
        System.out.println("Size of the set: " + studentSet.size());

        // Clearing the set
        studentSet.clear();
        System.out.println("Is the set empty now? " + studentSet.isEmpty());
    }
}

D:\>javac HashSetExample.java

D:\>java HashSetExample
Duplicate 'Mahesh' added? false

Set of students:
Ganesh
Datta
Mahesh
Harsha
Paani

Updated set of students:
Ganesh
Mahesh
Harsha
Paani

Does the set contain 'Harsha'? true
Size of the set: 4
Is the set empty now? trueCode language: JavaScript (javascript)

In Java programming, HashSet is a versatile and efficient data structure for managing collections of unique elements. Understanding its methods and usage allows developers to implement efficient solutions for tasks that require unique element storage and quick lookup operations. When uniqueness and fast access are priorities, HashSet proves to be a valuable choice.

Scroll to Top