In this article, we will see “How to iterate an Array using forEach statement in Java 8”. Here, we will go through several examples to understand this feature.

Iterate Array using Java 8 forEach...!!! Share on X

Example 1. Array with forEach (Java 8)

import java.util.Arrays;
import java.util.function.Consumer;

public class ForEach_Java8_Array_Example {

    public static void main(String[] args) {

        String[] names = {
            "noni",
            "yogi",
            "dv",
            "pulkit",
            "isha",
            "yashi",
            "yashu"
        };

        //Anonymous Class Implementation
        System.out.println("forEach Anonymous Class Implementation:\n");
        System.out.println("List names:\n ");
        Arrays.stream(names).forEach(new Consumer < String > () {
            @Override
            public void accept(String v) {
                System.out.println(v);
            }
        });

        //Lambda Implementation
        System.out.println("\nLambda Implementation:\n");
        System.out.println("List names: \n");
        Arrays.stream(names).forEach((v) -> {System.out.println(v);});

        //Method Reference Implementation
        System.out.println("\nMethod Reference Implementation:\n");
        System.out.println("List names: \n");
        Arrays.stream(names).forEach(System.out::println);
    }
}

Output:

forEach Anonymous Class Implementation:

List names:
 
noni
yogi
dv
pulkit
isha
yashi
yashu

Lambda Implementation:

List names: 

noni
yogi
dv
pulkit
isha
yashi
yashu

Method Reference Implementation:

List names: 

noni
yogi
dv
pulkit
isha
yashi
yashu

Example 2. Array with forEach (conditional) (Java 8)

import java.util.Arrays;

public class ForEach_Java8_Array_Conditional_Example {

    public static void main(String[] args) {

        String[] names = {
            "noni",
            "yogi",
            "dv",
            "pulkit",
            "isha",
            "yashi",
            "yashu"
        };

        //Array Conditional check
        System.out.println("Check the array if it contains a person with name 'pulkit':\n");
        Arrays.stream(names).forEach((name) -> {
            if (name.equals("pulkit")) {
                System.out.println("Expected name 'pulkit' is present in the array");
            }
        });

    }

}

Output:

Check the array if it contains a person with name 'pulkit':

Expected name 'pulkit' is present in the array

Iterate Array using Java 8 forEach...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts