New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java 8 forEach

In Java 8, a new method is introduced to traverse the elements which is the forEach() method. This method is added to the Iterable interface as a default method.

We can use this method to traverse collection(list, set) elements. Collection classes that extend the Iterable interface can use the forEach() method to iterate elements.

It is similar to for-loop in terms of iterating the elements but it is designed to iterate the collection elements/objects. It is helpful when we are working with stream that returns an iterable like list, set, etc. So we can iterate them instantly without knowing their size as we do in normal for-loop.

This method is added for functional programming in Java, So, we can use it in stream API to traverse elements in a functional style.

This method takes a single parameter which is a functional interface. So, you can pass parameters like lambda expression, method reference, etc as an argument.

Syntax

default void forEach(Consumer<? super T> action)

Parameters:

action - The action to be performed for each element.

Time for an Example: Traversing Collection Elements

Let's take an example to traverse elements of a collection. In this example, we are iterating elements of the array list by using the forEach() method, and lambda expression is used to print elements.

import java.util.ArrayList;
import java.util.List;
public class STDemo {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>(); 
		list.add("India");
		list.add("China");
		list.add("US");
		list.add("UK");
		list.forEach(country->System.out.println(country));
	}

}


India
China
US
UK

Example: Traversing using Method Reference

We can pass a method reference also to forEach() method to write code in functional style. In this example, the print() method is called using method reference and passed as an argument to the forEach() method.

import java.util.ArrayList;
import java.util.List;
public class STDemo {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>(); 
		list.add("India");
		list.add("China");
		list.add("US");
		list.add("UK");
		list.forEach(System.out::println);
	}

}


India
China
US
UK

Example: Traversing in Stream API

The forEach() is primarily used to operate over the streams due to its functional nature. Here we have an integer array and then we get stream object of it to traverse its elements using forEach() method.

import java.util.Arrays;
import java.util.stream.IntStream;
public class STDemo {
	public static void main(String[] args) {
		int[] arr = {2,5,36,9,8};
		IntStream vals = Arrays.stream(arr);
		vals.forEach(elements->System.out.println(elements));
		
	}
}


2
5
36
9
8

Stream forEachOrdered() Method

Stream provides one more method forEachOrdered() which is similar to forEach() except it guarantees for the traversing order. The stream operations do not guaranty the collection order. So, when we want to traverse collection in the specified order then use forEachOrdered() method. The syntax of the method is given below.

void forEachOrdered(Consumer<? super T> action)

Example: Traversing Stream Elements

In this example, we are traversing elements by both the methods forEach() and forEachOrdered() to check whether it provides results in an order. See the below example.

import java.util.Arrays;
import java.util.stream.IntStream;
public class STDemo {
	public static void main(String[] args) {
		int[] arr = {2,5,36,9,8};
		IntStream vals = Arrays.stream(arr);
		vals.parallel().forEach(val->System.out.print(val+" "));
		System.out.println();
		vals = Arrays.stream(arr);
		vals.parallel().forEachOrdered(val->System.out.print(val+" "));
		
	}
}


36 8 9 2 5
2 5 36 9 8