Java 8 forEach() Method

Last Updated : 6 Apr 2026

Java 8 forEach() method is used to iterate over elements of a collection using functional programming concepts. In this chapter, you will learn about the forEach() method, its signature and usages along with examples.

What is forEach() Method?

The forEach() method is introduced in Java 8 and it is used to iterate over elements of a collection in a simple way. It is defined in the Iterable interface that means all collection classes (like List, Set, etc.) can use this method.

Instead of writing long loops, we can use forEach() along with a lambda expression to perform operations on each element.

forEach() Method Signature

The syntax to use forEach() method is as follows:

Signature Explanation

  • default: The default keyword means the method has a default implementation inside the interface.
  • void: The void keyword represents the return type of the method. The forEach() method does not return any value; it only performs an action on each element.
  • forEach: This is the method name. It indicates that the method performs an operation on each element of the collection.
  • (Consumer<? super T> action): This is the parameter of the method:
    • Consumer is a functional interface from the java.util.function package.
    • It takes one input argument and returns no result.
    • We usually pass a lambda expression here.

Understanding Generics in Signature

  • T: Represents the type of elements stored in the collection.
  • ? super T: This is called a lower bounded wildcard. It allows the method to accept a Consumer of type T or its superclass.

Advantages Over the Traditional Loops

A key advantage of forEach() over traditional loops is its compactness and readability, as it uses lambda expressions to handle iteration logic that reduces the mental effort required to understand the code and makes the code easier to maintain.

Additionally, forEach() promotes a more functional programming style by encouraging separation of concerns and minimizing direct mutation.

The forEach() Method Examples

The following examples demonstrate how the forEach() method is used to iterate over elements in different ways.

Example 1: Iterating using Lambda Expression

In this example, the forEach() method is used with a lambda expression to print each element of the collection.

Output:

------------Iterating by passing lambda expression--------------
Football
Cricket
Chess
Hocky

Example 2: Iterating using Method Reference

In this example, the forEach() method is used with a method reference instead of a lambda expression.

Output:

------------Iterating by passing method reference---------------
Football
Cricket
Chess
Hocky

Using forEach() with Lambda Expression

In this approach, the forEach() method uses a lambda expression to define the operation that should be performed on each element.

Example

In this example, each element of the list is printed using a lambda expression.

Output:

Football
Cricket
Chess
Hockey

Using forEach() with Method Reference

In this approach, the forEach() method uses a method reference instead of a lambda expression.

Example

In this example, each element of the list is printed using a method reference.

Output:

Football
Cricket
Chess
Hockey