Java 8 provides one new method forEach() to iterate the elements over collections and Streams in Java. It is defined in Iterable and Stream interfaces. Collection classes which extends Iterable interface can use forEach method to iterate elements.

Here is example of forEach to iterate a Map.

package com.javatutsworld;

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

public class ForEachExample {

    public static void main(String a[]) {

        Map<Integer, String> employeeMap = new HashMap<>();
        employeeMap.put(1001, "Gaurav Pantola");
        employeeMap.put(1002, "Pardeep Kumar");
        employeeMap.put(1003, "Aariz");
        employeeMap.put(1004, "Saaim");

        // iterate through Map without Java 8
        System.out.println("iterate through Map without Java 8");
        for (Entry<Integer, String> entry : employeeMap.entrySet()) {
            System.out.println("Employee ID: " + entry.getKey() + " : Employee Name: " + entry.getValue());
        }
        System.out.println("\n");
        // iterate through Map using Java 8 forEach method
        System.out.println("iterate through Map using Java 8 forEach method");
        employeeMap.forEach((key, value) -> {
            System.out.println("Employee ID: " + key + "Employee Name: " + value);
        });
    }

}

Output:

iterate through Map without Java 8
Employee ID: 1001 : Employee Name: Gaurav Pantola
Employee ID: 1002 : Employee Name: Pardeep Kumar
Employee ID: 1003 : Employee Name: Aariz
Employee ID: 1004 : Employee Name: Saaim


iterate through Map using Java 8 forEach method
Employee ID: 1001Employee Name: Gaurav Pantola
Employee ID: 1002Employee Name: Pardeep Kumar
Employee ID: 1003Employee Name: Aariz
Employee ID: 1004Employee Name: Saaim

Here is example of forEach to iterate a List.

package com.javatutsworld;

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

public class ForEachExampleList {

    public static void main(String a[]) {

        List<String> employeeList = new ArrayList();
        employeeList.add("Gaurav Pantola");
        employeeList.add("Pardeep Kumar");
        employeeList.add("Aariz");
        employeeList.add("Saaim");

        // iterate through List without Java 8
        System.out.println("iterate through List without Java 8");
        for (String employee : employeeList) {
            System.out.println("Employee Name: " + employee);
        }
        System.out.println("\n");
        // iterate through List using Java 8 forEach method
        System.out.println("iterate through List using Java 8 forEach method");
        employeeList.forEach(employee -> {
            System.out.println("Employee Name: " + employee);
        });
    }

}

Output

iterate through List without Java 8
Employee Name: Gaurav Pantola
Employee Name: Pardeep Kumar
Employee Name: Aariz
Employee Name: Saaim


iterate through List using Java 8 forEach method
Employee Name: Gaurav Pantola
Employee Name: Pardeep Kumar
Employee Name: Aariz
Employee Name: Saaim

Please click below link for more details.

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html