Java forEach Example Using Lambda Expression

Introduction

This tutorial will show you how to use Java 8 forEach() loop to iterate Collection using Lambda Expression. Prior to Java 8 or JDK 8, the for loop was used as a for-each style but in Java 8 the inclusion of forEach() loop simplifies the iteration process in mainly one line.

Prerequisites

Java 1.8+, Maven 3.6.3 – 3.8.2

Project Setup

A maven based project is created and the following pom.xml file is used for this project.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-foreach-lambda-expression</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>16</maven.compiler.source>
		<maven.compiler.target>16</maven.compiler.target>
	</properties>

	<dependencies>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>

</project>

forEach() Example

Create the below class to iterate List and Map using forEach() loop.

public class LambdaForEachExample {

	public static void main(String[] args) {
		List<Integer> integers = new ArrayList<>();
		integers.add(1);
		integers.add(2);
		integers.add(3);
		integers.add(5);
		integers.add(7);
		integers.add(11);

		System.out.println("Iterating over List");
		System.out.println("-------------------");
		System.out.println();

		System.out.println("Accessing elements without using stream API");
		System.out.println("Print elements using method reference:");
		integers.forEach(System.out::println);

		System.out.println("Printing elements after multiplying each element by 2: ");
		integers.forEach(i -> System.out.print((i * 2) + ","));

		System.out.println("\n");

		System.out.println("Accessing elements using stream API");

		System.out.println("Print elements using method reference:");
		integers.stream().forEach(System.out::println);

		System.out.println("Printing elements after multiplying each element by 2: ");

		integers.stream().forEach(i -> System.out.print((i * 2) + ","));

		System.out.println();
		System.out.println("Printing elements using parallel stream, order is not guranteed: ");

		integers.parallelStream().forEach(i -> System.out.print((i * 2) + ","));

		System.out.println("\n");

		System.out.println("Iterating over Map");
		System.out.println("------------------");
		System.out.println();

		Map<String, String> map = new HashMap<>();
		map.put("a", "A");
		map.put("b", "B");
		map.put("c", "C");
		map.put("d", "D");
		map.put("e", "E");
		map.put("f", "F");
		map.put("g", "G");

		System.out.println("Print map elements");

		map.forEach((k, v) -> System.out.print(k + " : " + v + ", "));
	}

}

Testing the Program

Run the above class, you will see the below output in the console.

Iterating over List
-------------------

Accessing elements without using stream API
Print elements using method reference:
1
2
3
5
7
11
Printing elements after multiplying each element by 2: 
2,4,6,10,14,22,

Accessing elements using stream API
Print elements using method reference:
1
2
3
5
7
11
Printing elements after multiplying each element by 2: 
2,4,6,10,14,22,
Printing elements using parallel stream, order is not guranteed: 
10,14,22,2,4,6,

Iterating over Map
------------------

Print map elements
a : A, b : B, c : C, d : D, e : E, f : F, g : G,

Source Code

Download

Leave a Reply

Your email address will not be published.