Java LinkedHashSet iterator() Method

The LinkedHashSet.iterator() method in Java is used to obtain an iterator over the elements in a LinkedHashSet.

Table of Contents

  1. Introduction
  2. iterator Method Syntax
  3. Examples
    • Iterating Over Elements in a LinkedHashSet
    • Removing Elements Using Iterator
  4. Conclusion

Introduction

The LinkedHashSet.iterator() method is a member of the LinkedHashSet class in Java. It allows you to traverse the elements in the LinkedHashSet using an iterator.

iterator Method Syntax

The syntax for the iterator method is as follows:

public Iterator<E> iterator()
  • The method does not take any parameters.
  • The method returns an Iterator over the elements in the LinkedHashSet.

Examples

Iterating Over Elements in a LinkedHashSet

The iterator method can be used to obtain an iterator and iterate over the elements in a LinkedHashSet.

Example

import java.util.Iterator;
import java.util.LinkedHashSet;

public class IteratorExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Obtaining an iterator for the LinkedHashSet
        Iterator<String> iterator = animals.iterator();

        // Iterating over the elements in the LinkedHashSet
        while (iterator.hasNext()) {
            String animal = iterator.next();
            System.out.println("Animal: " + animal);
        }
    }
}

Output:

Animal: Lion
Animal: Tiger
Animal: Elephant

Removing Elements Using Iterator

You can also use the iterator to remove elements from the LinkedHashSet.

Example

import java.util.Iterator;
import java.util.LinkedHashSet;

public class RemoveIteratorExample {
    public static void main(String[] args) {
        // Creating a LinkedHashSet of Strings
        LinkedHashSet<String> animals = new LinkedHashSet<>();

        // Adding elements to the LinkedHashSet
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");

        // Obtaining an iterator for the LinkedHashSet
        Iterator<String> iterator = animals.iterator();

        // Removing elements while iterating
        while (iterator.hasNext()) {
            String animal = iterator.next();
            if ("Tiger".equals(animal)) {
                iterator.remove();
            }
        }

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

Output:

LinkedHashSet after removal: [Lion, Elephant]

Conclusion

The LinkedHashSet.iterator() method in Java provides a way to traverse the elements in a LinkedHashSet. By understanding how to use this method, you can efficiently iterate over and manipulate the elements in your collections. The method ensures that you can traverse the set in insertion order, making it useful for various operations in your Java applications.

Leave a Comment

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

Scroll to Top