The Java forEach method is a utility method that can be used to iterate over a Java Collection or a Stream. It is very handy while working with a Stream or any Java collection like List
, Map
, Set
, Stack
, Queue
, etc.
In this article, you’ll find examples of Java forEach method with List, Map, and Stream.
Java forEach method
The Java forEach method is defined in the Iterable
interface. Since the Collection
interface extends the Iterable
interface, this method is available to all the Java collection classes.
The forEach()
method takes a Consumer
as an argument and performs the action defined by the Consumer for every item in the collection.
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
A Consumer
is a functional interface. It can be created using Java’s anonymous class syntax or using a Lambda expression:
// Creating a consumer using the anonymous class syntax
Consumer<String> action = new Consumer<String>() {
@Override
public void accept(String s) {
// perform action
System.out.println(s);
}
};
// Creating a Consumer using a lambda expression
Consumer<String> action = s -> {
// perform action
System.out.println(s);
};
// The curly braces can be omitted if the lambda expression's body consists of a single line
Consumer<String> action = s -> System.out.println(s);
Let’s now see examples of forEach method with List, Map, and Stream.
Java List forEach example
import java.util.Arrays;
import java.util.List;
public class ListForEachExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("John", "Jack", "Sachin", "Mark");
// List forEach with lambda expression
names.forEach(name -> {
System.out.printf("Hello %s%n", name);
});
// List forEach with Method reference
names.forEach(System.out::println);
}
}
# Output
Hello John
Hello Jack
Hello Sachin
Hello Mark
John
Jack
Sachin
Mark
Java Map forEach example
import java.util.HashMap;
import java.util.Map;
public class MapForEachExample {
public static void main(String[] args) {
Map<Integer, String> employeeIdNameMapping = new HashMap<>();
employeeIdNameMapping.put(101, "Jack");
employeeIdNameMapping.put(102, "Maria");
employeeIdNameMapping.put(103, "Robert");
employeeIdNameMapping.put(104, "Joseph");
// Map forEach example
employeeIdNameMapping.forEach((id, name) -> {
System.out.printf("Employee ID: %s, Name: %s%n", id, name);
});
}
}
# Output
Employee ID: 101, Name: Jack
Employee ID: 102, Name: Maria
Employee ID: 103, Name: Robert
Employee ID: 104, Name: Joseph
Java Stream forEach example
The Java Stream forEach method is different from the Java Collections forEach method. It is defined in the Stream
interface. However, the Stream forEach method also accepts a Consumer
. So It works in the same way as the Collections forEach method. Let’s see an example:
import java.util.stream.Stream;
public class StreamForEachExample {
public static void main(String[] args) {
Stream<String> countries = Stream.of("India", "China", "United States", "United Kingdom", "Germany", "France");
// Stream forEach with lambda expression
countries.forEach(name -> {
System.out.printf("Hello %s%n", name);
});
Stream<String> programmingLanguages = Stream.of("Java", "Go", "Rust", "C++", "Swift");
// Stream forEach with Method reference
programmingLanguages.forEach(System.out::println);
Stream<Integer> numbers = Stream.of(2, 5, 7, 8, 10, 23, 44, 68);
// Stream forEach with filter
numbers.filter(num -> num % 2 == 0)
.forEach(num -> System.out.println(num));
}
}
# Output
Hello India
Hello China
Hello United States
Hello United Kingdom
Hello Germany
Hello France
Java
Go
Rust
C++
Swift
2
8
10
44
68