Guide to Java8 For Each Method

Published May 20, 2022

Iterating across Java maps, arrays, or collections has been made possible in a variety of methods. Iteration in Java was first accomplished via the usage of iterators. Iteration was accomplished later in Java 5 using the extended loop or For-each (for (String s: collection)), which removed the requirement for iterators. Finally, in Java 8, the forEach () method was added as a new, easy, and powerful solution to iterating across collections.

In this guide, we are going to learn various applications of the Java 8  forEach the () method.

 

The applications of the Java 8  forEach() method

The forEach () method in Java 8 has made it simple for developers to iterate across collections like sets, lists, and maps. Let's look at how developers use Java 8's forEach() function to traverse through the mentioned collections.

Application 1: Iterating a Set

This is the second application of the Java 8 forEach() method. Developers are able to Iterate over the sets in Java easily and fast

 

import java.util.HashSet;

import java.util.Set;

public class Iterate_list {

            public static void main(String[] args) {

        Set<String> planguages = new HashSet<>();

        planguages.add("Java");

        planguages.add("python");

        planguages.add("Kotlin");

        planguages.add("Bootstrap");

        planguages.forEach((e) -> { System.out.println(e); });

            }

}

 

Output:

Java

python

Bootstrap

Kotlin

 

Application 2: Iterating a List

The  Java 8 forEach() method allows the Java developers  to iterate over a List. let's take a look at the example below

 

import java.util.ArrayList;

import java.util.List;

public class Iterating_list {

            public static void main(String[] args) {

                        List<String> planguage = new ArrayList<>();

                        planguage.add("Java");

                        planguage.add("kotlin");

                        planguage.add("matlab");

                        planguage.add("Bootstrap");

 

                        planguage.stream().filter(item -> (item.length() == 4)).forEach(System.out::println);

 

            }

 

}

 

Output:

Java

 

Application 3: Using forEach() in an Array

To convert the array into a stream, Java developers utilize the Arrays.stream function. The forEach function then loops over the items and outputs their values to the console.

Examples of forEach on an Array

package foreach_array;

import java.util.Arrays;

public class foreach_array {

            public static void main(String[] args) {

         int[] numbersz = { 80, 70, 60, 50 };

       

        Arrays.stream(numbersz).forEach((e) -> { System.out.println(e); });

            }

}

 

80

70

60

50

 

Application 4: Filtering a List

In this application, We filter a list of strings in this application and display the filtered list to the console. Only strings of four characters are displayed. Consider the following example:

Example

In this example, we are going  to filter the items with four characters

 

package filtering_list;

import java.util.ArrayList;

import java.util.List;

public class Filtering_list {

            public static void main(String[] args) {

                        List<String> items = new ArrayList<>();

        items.add("Java");

        items.add("python");

        items.add("kotlin");

        items.add("css3");

        items.stream().filter(item -> (item.length() == 4)).forEach(System.out::println);

            }

}

 

Output:

Java

css3

 

Keywords: Guide to Java8 For Each Method , ForEach Loop

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

169 Views