forEach Method in Java

Naveen AnamaneniUpdated · 3 min read

The forEach method in Java is used to perform an action on every element of a collection. It takes a lambda (a Consumer) and calls it once for each element, so you don't have to write a loop. forEach was added in Java 8 to the Iterable interface (so every List, Set and Queue has it), to Map (with a two-argument lambda), and to streams. In this lesson, you will learn each version and when a normal loop is still the better choice.

Declaration

Declaration
// in Iterable<T>
default void forEach(Consumer<? super T> action)
 
// in Map<K, V>
default void forEach(BiConsumer<? super K, ? super V> action)
 
// in Stream<T>
void forEach(Consumer<? super T> action)

Example: forEach on a List, a Set and a Map

Let's see an example of each.

ForEachDemo.java▶ Try it
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
 
public class ForEachDemo {
    public static void main(String[] args) {
        List<String> names = List.of("Navika", "Srihitha", "Advitha");
        names.forEach(n -> System.out.println("Hello " + n));
 
        new TreeSet<>(names).forEach(System.out::println);
 
        Map<String, Integer> marks = new LinkedHashMap<>();
        marks.put("Navika", 91);
        marks.put("Srihitha", 78);
        marks.forEach((name, m) -> System.out.println(name + " scored " + m));
    }
}
Output
Hello Navika
Hello Srihitha
Hello Advitha
Advitha
Navika
Srihitha
Navika scored 91
Srihitha scored 78

Explanation: List.forEach runs the lambda for each name in order. TreeSet.forEach visits the names in sorted order, here with a method reference. Map.forEach passes the key and the value to a two-parameter lambda.

forEach vs a For-Each Loop

forEach(...) methodfor-each loop
break or continueNot possiblePossible
return from the enclosing methodNot possible (only from the lambda)Possible
Changing local variablesNot allowed (must be effectively final)Allowed
Checked exceptions insideMust be caught in the lambdaCan be thrown
Best forOne short action per elementAnything more complex

Example: Stream forEach and Ordering

StreamForEach.java▶ Try it
import java.util.List;
 
public class StreamForEach {
    public static void main(String[] args) {
        List<Integer> marks = List.of(91, 38, 85, 42);
 
        marks.stream()
             .filter(m -> m >= 50)
             .forEach(m -> System.out.println("Pass: " + m));
 
        marks.stream()
             .sorted()
             .forEachOrdered(m -> System.out.print(m + ";"));
        System.out.println();
    }
}
Output
Pass: 91
Pass: 85
38;42;85;91;

Explanation: forEach at the end of a stream acts on each element that survives the pipeline. forEachOrdered guarantees the stream's order even if the stream were parallel; on a parallel stream, plain forEach may visit elements in any order.

Common Mistakes

Calling list.remove(...) inside list.forEach(...). It throws ConcurrentModificationException; use removeIf.

Trying to break out of forEach. It isn't possible; use a loop or a stream with takeWhile or anyMatch.

Adding to an outside list from stream().forEach. Use collect or toList(), which also work correctly in parallel.

Practice

  1. Easy: Use forEach to print each element of a list of prices with a currency symbol.
  2. Harder: Given a Map<String, List<Integer>> of student marks, use Map.forEach to print each student's average.

Quick check: forEach

4 questions
1Which interface gives List its forEach method?
2How many parameters does the lambda for Map.forEach take?
3Can you use break inside forEach?
4Which keeps the order on a parallel stream?

Key Takeaways

Was this tutorial helpful?

Lesson 312 of 482