Java forEach

Java forEach is used to execute a set of statements for each element in the collection.

Java forEach function is defined in many interfaces. Some of the notable interfaces are Iterable, Stream, Map, etc.

In this Java Tutorial, we shall look into examples that demonstrate the usage of forEach(); function for some of the collections like List, Map and Set.

The syntax of foreach() function is:

</>
Copy
 elements.foreach(element -> {
     //set of statements
 });

where elements is the collection and set of statements are executed for each element. element could be accessed in the set of statements.

If there is only one statement you would like to execute for each element, you can write the code as shown below.

</>
Copy
elements.foreach(element -> statement);

Example 1 – Java forEach – List

In this example, we shall take Java List and write two forEach statements for the list. In the first forEach, we shall execute a single statement, like printing the value. In the second forEach statement, we shall execute an if-else loop for each of the element in the list.

Example.java

</>
Copy
import java.util.List;
import java.util.ArrayList;

public class Example {

	public static void main(String[] args) {
	
		List<String> nameList = new ArrayList<String>();
		
		nameList.add("Java");
		nameList.add("Kotlin");
		nameList.add("Android");
		
		// only single statement to be executed for each item in the ArrayList
		nameList.forEach(name -> System.out.println(name) );
		
		// multiple statements to be executed for each item in the ArrayList
		nameList.forEach(name -> {
			if(name.equals("Android")) {
				System.out.println(name + " is a framework.");	
			} else {
				System.out.println(name + " is a programming language.");
			}
		});
	}
}

Output

Java
Kotlin
Android
Java is a programming language.
Kotlin is a programming language.
Android is a framework.

Example 2 – Java forEach – Set

In this example, we have taken a Java Set, and executed a single statement for each of the element in the list using first forEach. And in the second forEach, we have executed an if-else statement for each of the element in the set.

Example.java

</>
Copy
import java.util.HashSet;
import java.util.Set;

public class Example {

	public static void main(String[] args) {
	
		Set<String> nameList = new HashSet<String>();
		
		nameList.add("Java");
		nameList.add("Kotlin");
		nameList.add("Android");
		
		// only single statement to be executed for each item in the HashSet
		nameList.forEach(name -> System.out.println(name) );
		
		// multiple statements to be executed for each item in the HashSet
		nameList.forEach(name -> {
			if(name.equals("Android")) {
				System.out.println(name + " is a framework.");	
			} else {
				System.out.println(name + " is a programming language.");
			}
		});
	}
}

Output

Java
Kotlin
Android
Java is a programming language.
Kotlin is a programming language.
Android is a framework.

Example 3 – Java forEach – Map

In this example, we used Java forEach function on the elements of Map.

Example.java

</>
Copy
import java.util.*;

public class Example {

	public static void main(String[] args) {
	
		Map<Integer, String> map = new HashMap<Integer, String>();
		
		map.put(10, "Java");
		map.put(20, "Kotlin");
		map.put(30, "Android");
		
		// only single statement to be executed for each item in the Map
		map.forEach( (key,value) -> System.out.println(key+":"+value) );
		
		// multiple statements to be executed for each item in the Map
		map.forEach((key,value) -> {
			if(value.equals("Android")) {
				System.out.println(value + " is a framework.");	
			} else {
				System.out.println(value + " is a programming language.");
			}
		});
	}
}

Output

20:Kotlin
10:Java
30:Android
Kotlin is a programming language.
Java is a programming language.
Android is a framework.

Conclusion

In this Java Tutorial, we learned how to use Java forEach function on some of the commonly used collections in Java application development.