Sunday, 19 July 2015

Java8: for loop to forEach function

Java8 streams provide forEach function, which performs an action for each element of this stream.

Normal for loop
for (Employee emp : employees) {
         System.out.println(emp);
}

….can be translated easily into forEach statement like following.

employees.stream().forEach(System.out::println);

Find following sample application.

import java.util.Objects;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private int age;
 private String city;
 private double salary;

 public Employee(int id, String firstName, String lastName, int age,
   String city, double salary) {
  super();
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.city = city;
  this.salary = salary;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

 @Override
 public boolean equals(Object employee) {
  if (Objects.isNull(employee))
   return false;

  if (!(employee instanceof Employee))
   return false;

  Employee emp = (Employee) employee;

  return id == emp.id;
 }

 @Override
 public int hashCode() {
  return Objects.hash(id, firstName, lastName, age);
 }

 @Override
 public String toString() {
  return String.format("%s(%s,%d,%f)", firstName, city, age, salary);
 }

}

import java.util.ArrayList;
import java.util.List;

public class Test {
 public static List<Employee> getEmployees() {
  Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26,
    "Bangalore", 40000);
  Employee emp2 = new Employee(2, "Joel", "Chelli", 27, "Hyderabad",
    50000);
  Employee emp3 = new Employee(3, "Shanmukh", "Kummary", 28, "Chennai",
    35000);
  Employee emp4 = new Employee(4, "Harika", "Raghuram", 27, "Chennai",
    76000);
  Employee emp5 = new Employee(5, "Sudheer", "Ganji", 27, "Bangalore",
    90000);

  List<Employee> employees = new ArrayList<>();

  employees.add(emp1);
  employees.add(emp2);
  employees.add(emp3);
  employees.add(emp4);
  employees.add(emp5);

  return employees;
 }

 public static void main(String args[]) {
  List<Employee> employees = getEmployees();

  for (Employee emp : employees) {
   System.out.println(emp);
  }

  System.out.println("\n***************");
  employees.stream().forEach(System.out::println);
  System.out.println("\n***************");

  /* Print all firstNames of employees */
  employees.stream().forEach(
    (emp -> (System.out.println(emp.getFirstName()))));

 }
}


Output

Hari Krishna(Bangalore,26,40000.000000)
Joel(Hyderabad,27,50000.000000)
Shanmukh(Chennai,28,35000.000000)
Harika(Chennai,27,76000.000000)
Sudheer(Bangalore,27,90000.000000)

***************
Hari Krishna(Bangalore,26,40000.000000)
Joel(Hyderabad,27,50000.000000)
Shanmukh(Chennai,28,35000.000000)
Harika(Chennai,27,76000.000000)
Sudheer(Bangalore,27,90000.000000)

***************
Hari Krishna
Joel
Shanmukh
Harika
Sudheer



No comments:

Post a Comment