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

Iterate Map & List using Java 8 forEach...!!! Share on X

Example 1. Map with forEach

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class ForEach_Java8_Map_Example {

    public static void main(String[] args) {

        Map < Integer, String > map = new HashMap < Integer, String > ();

        map.put(65, "noni");
        map.put(33, "yogi");
        map.put(73, "dv");
        map.put(46, "pulkit");
        map.put(37, "isha");
        map.put(98, "yashi");
        map.put(54, "yashu");

        System.out.println("forEach Anonymous Class Implementation:\n");
        map.forEach(new BiConsumer < Integer, String > () {
            @Override
            public void accept(Integer k, String v) {
                System.out.println("Key: " + k + " Value: " + v);
            }
        });

        System.out.println("\nforEach Lambda Implementation:\n");
        map.forEach((k, v) - > System.out.println("Key: " + k + " Value: " + v));

    }

}

 

Output:

forEach Anonymous Class Implementation:

Key: 65 Value: noni
Key: 33 Value: yogi
Key: 98 Value: yashi
Key: 37 Value: isha
Key: 54 Value: yashu
Key: 73 Value: dv
Key: 46 Value: pulkit

forEach Lambda Implementation:

Key: 65 Value: noni
Key: 33 Value: yogi
Key: 98 Value: yashi
Key: 37 Value: isha
Key: 54 Value: yashu
Key: 73 Value: dv
Key: 46 Value: pulkit

 

Example 2. Map with forEach (conditional)

import java.util.HashMap;
import java.util.Map;

public class ForEach_Java8_Map_Conditional_Example {

    public static void main(String[] args) {

        Map < Integer, String > users = new HashMap < Integer, String > ();

        users.put(65, "noni");
        users.put(33, "yogi");
        users.put(73, "dv");
        users.put(46, "pulkit");
        users.put(37, "isha");
        users.put(98, "yashi");
        users.put(54, "yashu");

        //Map Conditional check
        System.out.println("Print users whose name starts with 'y' :\n");
        users.forEach((id, name) - > {

            if (name.startsWith("y")) {
                System.out.println("Key: " + id + " Value: " + name);
            }

        });

    }

}

 

Output:

Print users whose name starts with 'y' :

Key: 33 Value: yogi
Key: 98 Value: yashi
Key: 54 Value: yashu

 

Example 3. List with forEach

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ForEach_Java8_List_Example {

    public static void main(String[] args) {

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

        users.add("noni");
        users.add("yogi");
        users.add("dv");
        users.add("pulkit");
        users.add("isha");
        users.add("yashi");
        users.add("yashu");

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

        //Lambda Implementation
        System.out.println("\nforEach Lambda Implementation:\n");
        users.forEach((v) - > System.out.println("List user names:  " + v));

        //Java 8 Method Reference
        System.out.println("\nJava 8 Method Reference Implementation:\n");
        System.out.println("List user names: \n");
        users.forEach(System.out::println);


        //Java 8 Stream Implementation
        System.out.println("\nJava 8 Stream Implementation Implementation:\n");
        users.stream().forEach((v) - > System.out.println("List user names:  " + v));

    }

}

 

Output:

forEach Anonymous Class Implementation:

List user names: noni
List user names: yogi
List user names: dv
List user names: pulkit
List user names: isha
List user names: yashi
List user names: yashu

forEach Lambda Implementation:

List user names:  noni
List user names:  yogi
List user names:  dv
List user names:  pulkit
List user names:  isha
List user names:  yashi
List user names:  yashu

Java 8 Method Reference Implementation:

List user names: 

noni
yogi
dv
pulkit
isha
yashi
yashu

Java 8 Stream Implementation Implementation:

List user names:  noni
List user names:  yogi
List user names:  dv
List user names:  pulkit
List user names:  isha
List user names:  yashi
List user names:  yashu

 

Example 4. List with forEach (conditional)

import java.util.ArrayList;
import java.util.List;

public class ForEach_Java8_List_Conditional_Example4 {

    public static void main(String[] args) {

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

        users.add("noni");
        users.add("yogi");
        users.add("dv");
        users.add("pulkit");
        users.add("isha");
        users.add("yashi");
        users.add("yashu");

        //List Conditional check
        System.out.println("Check the list if contains the user with name 'pulkit':\n");
        users.forEach((name) - > {
            if (name.equals("pulkit")) {
                System.out.println("Expected name 'pulkit' is present in the list");
            }
        });

    }

}

 

Output:

Check the list if contains the user with name 'Tommy':

Expected name 'pulkit' is present in the list

 

Iterate Map & List 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