Java HashSet CRUD Operations Example

1. Introduction

A HashSet in Java is a collection that does not allow duplicate elements and provides constant time performance for basic operations such as add, remove, and contains, assuming the hash function disperses elements properly among the buckets. This makes HashSet an excellent choice for collections where unique elements are a priority, and order is not a concern. This blog post will explore CRUD (Create, Read, Update, Delete) operations in a HashSet.

2. Program Steps

1. Create a new HashSet.

2. Add elements to the HashSet (Create).

3. Read/Check if elements are present in the HashSet.

4. Update an element in the HashSet (remove the old element and add a new one).

5. Remove an element from the HashSet (Delete).

3. Code Program

import java.util.HashSet;

public class HashSetCRUDExample {
    public static void main(String[] args) {
        // Step 1: Creating a new HashSet
        HashSet<String> fruits = new HashSet<>();

        // Step 2: Adding elements to the HashSet (Create)
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        System.out.println("HashSet after adding elements: " + fruits);

        // Step 3: Reading/Checking if elements are present in the HashSet
        boolean hasApple = fruits.contains("Apple");
        System.out.println("Contains Apple? " + hasApple);

        // Step 4: Updating an element in the HashSet (No direct method to update)
        // Remove the old element and add the new element
        if (fruits.remove("Banana")) {
            fruits.add("Blueberry");
            System.out.println("HashSet after updating (replacing Banana with Blueberry): " + fruits);
        }

        // Step 5: Removing an element from the HashSet (Delete)
        fruits.remove("Cherry");
        System.out.println("HashSet after removing Cherry: " + fruits);
    }
}

Output:

HashSet after adding elements: [Apple, Banana, Cherry]
Contains Apple? true
HashSet after updating (replacing Banana with Blueberry): [Apple, Blueberry, Cherry]
HashSet after removing Cherry: [Apple, Blueberry]

Explanation:

1. The program starts by creating a HashSet named fruits that stores String objects. HashSet ensures that all elements are unique.

2. Adding elements: The add() method is used to insert new elements. If an element already exists, add() does not modify the set.

3. Reading/Checking elements: The contains() method checks if a specific element is present in the set, demonstrating how to read or verify elements in a HashSet.

4. Updating elements: HashSet does not have a direct method for updating elements because it does not maintain element order or indices. To "update" an element, you remove the old element and add the new one. This example replaces "Banana" with "Blueberry".

5. Removing elements: The remove() method deletes a specified element from the set.

6. The output illustrates the state of the HashSet after each operation. This example demonstrates how to perform CRUD operations in a HashSet, highlighting its use cases for managing collections of unique elements.

Comments