Java sforEach method

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java forEach loop

Java provides a new method forEach() to iterate the elements. It is defined in Iterable and Stream interface. It is a default method defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach loop to iterate elements.

This method takes a single parameter which is a functional interface. So, you can pass lambda expression as an argument.


forEach() Signature in Iterable Interface

  1. default void forEach(Consumeraction)  


Java 8 forEach() example 1

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. public class ForEachExample {  
  4.     public static void main(String[] args) {  
  5.         List<String> gamesList = new ArrayList<String>();  
  6.         gamesList.add("Football");  
  7.         gamesList.add("Cricket");  
  8.         gamesList.add("Chess");  
  9.         gamesList.add("Hocky");  
  10.         System.out.println("------------Iterating by passing lambda expression--------------");  
  11.         gamesList.forEach(games -> System.out.println(games));  
  12.           
  13.     }  
  14. }  

Output:

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

Java 8 forEach() example 2

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. public class ForEachExample {  
  4.     public static void main(String[] args) {  
  5.         List<String> gamesList = new ArrayList<String>();  
  6.         gamesList.add("Football");  
  7.         gamesList.add("Cricket");  
  8.         gamesList.add("Chess");  
  9.         gamesList.add("Hocky");  
  10.         System.out.println("------------Iterating by passing method reference---------------");  
  11.         gamesList.forEach(System.out::println);  
  12.     }  
  13. }  

Output:

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

Java Stream forEachOrdered() Method

Along with forEach() method, Java provides one more method forEachOrdered(). It is used to iterate elements in the order specified by the stream.

Singnature:

  1. void forEachOrdered(Consumer action)  


Java Stream forEachOrdered() Method Example

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. public class ForEachOrderedExample {  
  4.     public static void main(String[] args) {  
  5.         List<String> gamesList = new ArrayList<String>();  
  6.         gamesList.add("Football");  
  7.         gamesList.add("Cricket");  
  8.         gamesList.add("Chess");  
  9.         gamesList.add("Hocky");  
  10.         System.out.println("------------Iterating by passing lambda expression---------------");  
  11.         gamesList.stream().forEachOrdered(games -> System.out.println(games));  
  12.         System.out.println("------------Iterating by passing method reference---------------");  
  13.         gamesList.stream().forEachOrdered(System.out::println);  
  14.     }  
  15.   
  16. }  

Output:

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