SEARCH BY

All

  • All
  • Java8
  • Spring

forEach() method in java8

Post a Comment
In this tutorial iam sharing explanation about forEach() with examples
We can pass Consumer action as a parameter to forEach() method.
There are different ways to create consumer action
1) by creating a class which implements Consumer interface by overriding action method
import java.util.function.Consumer;
public class EmpConsumer implements Consumer<Employee>{
 @Override
 public void accept(Employee emp) {
  // TODO Auto-generated method stub
  System.out.println("Employee Id: "+ emp.id);
  System.out.println("Employee First Name: "+ emp.fname);
  System.out.println("Employee Second Name: "+ emp.sname);
  System.out.println("_____________________________________");
 }
}
2) We can use the above Consumer class object as an action to forEach()
import java.util.List;
public class ForEachExample {
 public static void main(String a[])
 {
  // Dummy Employees data
  List<Employee> empList= Employee.getEmployessSamples();
  // Creating Conusumer Implementd Class Object
  EmpConsumer action= new EmpConsumer();
  // call forEach on empList collection
  empList.forEach(action);
 }
}

Now we will see another way to use action with forEach() method, that is directly we can crate Consumer object inline where ever we required.

List<Employee> empList=Employee.getEmpList();
  Consumer<Employee> displayEmpDetails=emp -> {
   System.out.println("Employee Id: "+emp.empId);
   System.out.println("Employee First Name: "+emp.fname);
   System.out.println("Employee Second Name: "+emp.sname);
   System.out.println("********************");
  };
empList.forEach(displayEmpDetails);

Using andThen(Consumer_Object) with Consumer

andThen is the method of Consumer Interface

following is the example with andThen()

package java8examples;

import java.util.List;
import java.util.function.Consumer;

public class ForEachExample {
 
 public static void main(String a[])
 {
  
  
  
  List<Employee> empList= Employee.getEmployessSamples();
  
  // Creating Conusumer Implementd Class Object
  
  EmpConsumer action= new EmpConsumer();
  
  Consumer<Employee> action1= (Employee e) -> System.out.println( e.id);
  
  // call forEach on empList collection
  
  empList.forEach(action1);
  
  
  Consumer<Employee> displayEmpDetails=emp -> {
      System.out.println("Employee Id: "+emp.id);
      System.out.println("Employee First Name: "+emp.fname);
      System.out.println("Employee Second Name: "+emp.sname);
      System.out.println("********************");
     };
   empList.forEach(displayEmpDetails.andThen(action1));
 }

}


// output

/*

1
2
3
Employee Id: 1
Employee First Name: jai
Employee Second Name: aKumar
********************
1
Employee Id: 2
Employee First Name: jai
Employee Second Name: kumar1
********************
2
Employee Id: 3
Employee First Name: suresh
Employee Second Name: Nath
********************
3
 

*/


References: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/function/Consumer.html


jaya
I love software designing, coding, writing and teaching...

Share this article

Prev Thats All

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox