Iterating Over a HashSet Using an Iterator in Java

In this article, we will discuss how to iterate over a HashSet using an Iterator in Java. We will cover the basics of using an Iterator, providing examples to demonstrate its usage.

Table of Contents

  1. Introduction
  2. Using an Iterator to Iterate Over a HashSet
  3. Removing Elements During Iteration
  4. Complete Example
  5. Conclusion

Introduction

A HashSet in Java is a part of the Java Collections Framework and implements the Set interface. It allows for efficient storage and retrieval of unique elements. Using an Iterator to traverse a HashSet provides a flexible way to access each element and optionally remove elements during iteration.

Using an Iterator to Iterate Over a HashSet

The Iterator interface provides methods to iterate over a collection. The hasNext() method checks if there are more elements to iterate, and the next() method returns the next element in the iteration.

Example

import java.util.HashSet;
import java.util.Iterator;

public class IterateUsingIterator {
    public static void main(String[] args) {
        HashSet<String> fruits = new HashSet<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Creating an iterator
        Iterator<String> iterator = fruits.iterator();

        // Iterating over the HashSet
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            System.out.println("Fruit: " + fruit);
        }
    }
}

Output:

Fruit: Apple
Fruit: Banana
Fruit: Orange

Removing Elements During Iteration

The Iterator interface also provides a remove() method, which can be used to remove elements from the collection during iteration. This is particularly useful when you need to filter elements based on certain criteria.

Example

import java.util.HashSet;
import java.util.Iterator;

public class RemoveDuringIteration {
    public static void main(String[] args) {
        HashSet<String> fruits = new HashSet<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Banana");

        // Creating an iterator
        Iterator<String> iterator = fruits.iterator();

        // Iterating and removing "Banana" from the HashSet
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            if ("Banana".equals(fruit)) {
                iterator.remove();
            }
        }

        // Printing the HashSet after removal
        System.out.println("HashSet after removal: " + fruits);
    }
}

Output:

HashSet after removal: [Apple, Orange]

Complete Example

Here is a complete example that demonstrates both iterating over a HashSet and removing elements during iteration.

import java.util.HashSet;
import java.util.Iterator;

public class HashSetIteratorExample {
    public static void main(String[] args) {
        HashSet<String> fruits = new HashSet<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Banana");

        // Creating an iterator
        Iterator<String> iterator = fruits.iterator();

        // Iterating over the HashSet
        System.out.println("Iterating over the HashSet:");
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            System.out.println("Fruit: " + fruit);
        }

        // Re-create iterator for removal example
        iterator = fruits.iterator();

        // Iterating and removing "Banana" from the HashSet
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            if ("Banana".equals(fruit)) {
                iterator.remove();
            }
        }

        // Printing the HashSet after removal
        System.out.println("HashSet after removal: " + fruits);
    }
}

Output:

Iterating over the HashSet:
Fruit: Apple
Fruit: Banana
Fruit: Orange
HashSet after removal: [Apple, Orange]

Conclusion

Using an Iterator to traverse a HashSet in Java provides a flexible way to access and manipulate elements. This guide provided examples to demonstrate how to use an Iterator to iterate over a HashSet and remove elements during iteration. By understanding these concepts, you can efficiently manage elements in your HashSet.

Comments