Java LinkedHashSet forEach() Method

The LinkedHashSet.forEach() method in Java is used to perform an action for each element in the LinkedHashSet

Table of Contents

  1. Introduction
  2. forEach Method Syntax
  3. Examples
    • Iterating Over Elements in LinkedHashSet
    • Performing Actions on Elements
  4. Conclusion

Introduction

The LinkedHashSet.forEach() method is a member of the LinkedHashSet class in Java. It allows you to perform a specified action for each element in the LinkedHashSet. This method is part of the Iterable interface and is commonly used to iterate over the elements in a collection.

forEach() Method Syntax

The syntax for the forEach method is as follows:

public void forEach(Consumer<? super E> action)
  • The method takes a single parameter action of type Consumer<? super E>, which represents the action to be performed for each element.
  • The method does not return any value.

Examples

Iterating Over Elements in LinkedHashSet

The forEach method can be used to iterate over the elements in a LinkedHashSet.

Example

import java.util.LinkedHashSet;

public class ForEachExample {
    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");

        // Using forEach to iterate over the elements and print them
        animals.forEach(animal -> System.out.println("Animal: " + animal));
    }
}

Output:

Animal: Lion
Animal: Tiger
Animal: Elephant

Performing Actions on Elements

You can use the forEach method to perform various actions on the elements of the LinkedHashSet.

Example

import java.util.LinkedHashSet;

public class ActionExample {
    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");
        animals.add("Giraffe");
        animals.add("Zebra");

        // Using forEach to convert elements to uppercase and print them
        animals.forEach(animal -> {
            String upperCaseAnimal = animal.toUpperCase();
            System.out.println("Uppercase Animal: " + upperCaseAnimal);
        });
    }
}

Output:

Uppercase Animal: LION
Uppercase Animal: TIGER
Uppercase Animal: ELEPHANT
Uppercase Animal: GIRAFFE
Uppercase Animal: ZEBRA

Conclusion

The LinkedHashSet.forEach() method in Java provides a way to perform a specified action for each element in a LinkedHashSet. By understanding how to use this method, you can efficiently iterate over and manipulate the elements in your collections. This method is useful for processing collections in a concise and functional style, making it a valuable tool for managing data in your Java applications.

Comments