Java streams question on map and filter

package streams;

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

public class StreamQuestions {

  public static void main(String[] args) {

    List < Employee > employeeList = new ArrayList < > () {
      {
        add(new Employee(101 L, "Amit Aswal", 10000, "India"));
        add(new Employee(102 L, "Amit Rawat", 20000, "USA"));
        add(new Employee(103 L, "Vikas Rana", 24000, "India"));
        add(new Employee(104 L, "Dheeraj Pundir", 45343, "USA"));
        add(new Employee(105 L, "Anuj Rana", 200000, "India"));
      }
    };

    // Printing all the employees
    employeeList.stream().forEach(System.out::println);

    System.out.println("\n\n(Salary > 10000)");
    // Priniting employes where salary > 10000
    employeeList.stream().filter(employee -> employee.getSalary() > 10000)
      .forEach(System.out::println);

    System.out.println("\n\n(Printing name where Salary > 10000)");
    employeeList.stream().filter(employee -> employee.getSalary() > 10000)
      .map(employee -> employee.getName())
      .forEach(System.out::println);

    System.out.println("\n\n(Printing name where sorted Salary > 10000)");
    employeeList.stream().filter(employee -> employee.getSalary() > 10000)
      .map(employee -> employee.getName())
      .sorted()
      .forEach(System.out::println);

    // adding 100 to salaries of all employees
    System.out.println("\n\n(Adding 100 to slaries of all employees > 10000)");
    employeeList.stream().map(employee -> {
        employee.setSalary(employee.getSalary() + 100);
        return employee;
      })
      .forEach(System.out::println);
  }
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *