What is HashSet in Java? Explain with example

HashSet in Java

HashSet in Java: A HashSet in Java is a part of the Java Collections Framework. It implements the Set interface and is backed by a HashMap. Here are some key points about HashSet:

  • Unique Elements: A HashSet contains only unique elements. It does not allow duplicate values.
  • No Order Guarantee: The elements in a HashSet are not ordered. The set does not maintain the order in which elements were inserted.
  • Allows Null: A HashSet allows the insertion of one null element.
  • Fast Access: It provides constant-time performance for basic operations like add, remove, contains, and size, assuming the hash function disperses elements properly among the buckets.

Here’s a simple example to illustrate the use of HashSet:

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        // Create a HashSet
        HashSet<String> fruits = new HashSet<>();
        
        // Add elements to the HashSet
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Apple"); // Duplicate element
        
        // Print the HashSet
        System.out.println("HashSet: " + fruits);
        
        // Check if an element exists in the HashSet
        if (fruits.contains("Banana")) {
            System.out.println("Banana is in the HashSet.");
        }
        
        // Remove an element from the HashSet
        fruits.remove("Orange");
        System.out.println("HashSet after removing Orange: " + fruits);
        
        // Iterate over the elements in the HashSet
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

Explanation

Creating a HashSet in Java:

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

This line creates a HashSet object named fruits that will store strings.

Adding Elements:

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple");

These lines add elements to the HashSet. Notice that “Apple” is added twice. However, since a HashSet does not allow duplicate elements, only one “Apple” will be stored.

Printing the HashSet in Java:

System.out.println("HashSet: " + fruits);

This line prints the HashSet. The order of elements is not guaranteed.

Checking for an Element:

if (fruits.contains("Banana")) {
    System.out.println("Banana is in the HashSet.");
}

This checks if “Banana” is present in the HashSet.

Removing an Element:

fruits.remove("Orange");
System.out.println("HashSet after removing Orange: " + fruits);

This removes “Orange” from the HashSet and prints the updated set.

Iterating Over the HashSet in Java:

for (String fruit : fruits) {
    System.out.println(fruit);
}

This iterates over all elements in the HashSet and prints each one.

This example demonstrates the basic operations you can perform with a HashSet in Java.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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