Saturday, July 20, 2019

Stream Operation On Object In Java

In This example i am going to do some different stream operation on Employee Class Object.

package com.shubh.stream.api;

import java.util.Date;

public class Employee {

 private int id;
 private String name;
 private int age;
 private String email;
 private Date dateOfJoining;
 public String department;
 public int salary;

 public Employee(int id, String name, int age, String email, Date dateOfJoining, int salary) {
  this.id = id;
  this.name = name;
  this.age = age;
  this.email = email;
  this.dateOfJoining = dateOfJoining;
  this.salary = salary;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

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

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public Date getDateOfJoining() {
  return dateOfJoining;
 }

 public void setDateOfJoining(Date dateOfJoining) {
  this.dateOfJoining = dateOfJoining;
 }

 public int getId() {
  return id;
 }

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

 public String getDepartment() {
  return department;
 }

 public void setDepartment(String department) {
  this.department = department;
 }

 public int getSalary() {
  return salary;
 }

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

 @Override
 public String toString() {
  return "Employee{ id " + id + ", name='" + name + '\'' + ", age=" + age + ", email " + email + ", DOJ "
    + dateOfJoining + ", Salary " + salary + "}";
 }
}
Now Create different stream operations on Employee Class Objects like sorting by name, age, DateOfJoining etc.

package com.shubh.stream.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamApplication {

 public static void main(String[] args) throws ParseException {

  // Employee(int id, String name, int age,String email, Date dateOfJoining);
  SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
  List Employees = Arrays.asList(
    new Employee(1, "C", 30, "C@xyz.com", formatter.parse("01/08/1999"), 15000),
    new Employee(2, "A", 40, "A@xyz.com", formatter.parse("01/08/1955"), 5000),
    new Employee(3, "A", 10, "A1@xyz.com", formatter.parse("01/08/1979"), 3000),
    new Employee(3, "A", 10, "A2@xyz.com", formatter.parse("01/08/1978"), 2000),
    new Employee(4, "B", 20, "B@xyz.com", formatter.parse("01/08/1989"), 2000),
    new Employee(5, "E", 50, "E@xyz.com", formatter.parse("01/08/1985"), 10000));

  /*
   * List sortedList = Employees.stream() .sorted((o1, o2) -> o1.getAge() - o2.getAge()) .collect(Collectors.toList());
   */

  System.out.println("//Sort by Age.");
  List sortedByAge = Employees.stream().sorted(Comparator.comparingInt(Employee::getAge))
    .collect(Collectors.toList());
  sortedByAge.forEach(System.out::println);

  System.out.println("\n//Sort by Name.");

  /*
   * List sortedList = Employees.stream() .sorted((o1, o2) -> o1.getName().compareTo(o2.getName())) .collect(Collectors.toList());
   */

  List sorteByName = Employees.stream().sorted(Comparator.comparing(Employee::getName))
    .collect(Collectors.toList());
  sorteByName.forEach(System.out::println);

  // Sort by Name then Age.
  System.out.println("\n//Sort by Name then Age.");
  List sorteByNameThenAge = Employees.stream()
    .sorted(Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge))
    .collect(Collectors.toList());
  sorteByNameThenAge.forEach(System.out::println);

  // Sort by Name then Age then dateOfJoining.
  System.out.println("\n//Sort by Name then Age then dateOfJoining.");
  List sorteByNameThenAgeThenDoj = Employees.stream().sorted(Comparator.comparing(Employee::getName)
    .thenComparingInt(Employee::getAge).thenComparing(Employee::getDateOfJoining))
    .collect(Collectors.toList());
  sorteByNameThenAgeThenDoj.forEach(System.out::println);

  // Salary > 10000.
  System.out.println("\n//Salary > 10000");
  // find employees whose salaries are above 10000
  /* Filter and print
   * Employees.stream().filter(emp->emp.getSalary() > 10000).forEach(System.out::println);
   */
  /* filter and collect in List
   * List salaryGT10000 = Employees.stream().filter(emp ->emp.getSalary() > 10000) .collect(Collectors.toList());
   */
  List salaryGT10000 = Employees.stream().filter(emp -> emp != null)
    .filter(emp -> emp.getSalary() > 10000).collect(Collectors.toList());
  salaryGT10000.forEach(System.out::println);

  // find max sarary
  // Optional maxSalary = Employees.stream().max(Comparator.comparing(Employee::getSalary));
  // System.out.println(maxSalary);
  Employee maxSalary1 = Employees.stream().max(Comparator.comparing(Employee::getSalary))
    .orElseThrow(NoSuchElementException::new);
  System.out.println("Max salary of employee: " + maxSalary1);

  Employee minSalary1 = Employees.stream().min(Comparator.comparing(Employee::getSalary))
    .orElseThrow(NoSuchElementException::new);
  System.out.println("Min salary of employee: " + minSalary1);
 }
}
Output of above operations.

//Sort by Age.
Employee{ id 3, name='A', age=10, email A1@xyz.com, DOJ Mon Jan 08 00:00:00 IST 1979, Salary 3000}
Employee{ id 3, name='A', age=10, email A2@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1978, Salary 2000}
Employee{ id 4, name='B', age=20, email B@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1989, Salary 2000}
Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Employee{ id 2, name='A', age=40, email A@xyz.com, DOJ Sat Jan 08 00:00:00 IST 1955, Salary 5000}
Employee{ id 5, name='E', age=50, email E@xyz.com, DOJ Tue Jan 08 00:00:00 IST 1985, Salary 10000}

//Sort by Name.
Employee{ id 2, name='A', age=40, email A@xyz.com, DOJ Sat Jan 08 00:00:00 IST 1955, Salary 5000}
Employee{ id 3, name='A', age=10, email A1@xyz.com, DOJ Mon Jan 08 00:00:00 IST 1979, Salary 3000}
Employee{ id 3, name='A', age=10, email A2@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1978, Salary 2000}
Employee{ id 4, name='B', age=20, email B@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1989, Salary 2000}
Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Employee{ id 5, name='E', age=50, email E@xyz.com, DOJ Tue Jan 08 00:00:00 IST 1985, Salary 10000}

//Sort by Name then Age.
Employee{ id 3, name='A', age=10, email A1@xyz.com, DOJ Mon Jan 08 00:00:00 IST 1979, Salary 3000}
Employee{ id 3, name='A', age=10, email A2@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1978, Salary 2000}
Employee{ id 2, name='A', age=40, email A@xyz.com, DOJ Sat Jan 08 00:00:00 IST 1955, Salary 5000}
Employee{ id 4, name='B', age=20, email B@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1989, Salary 2000}
Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Employee{ id 5, name='E', age=50, email E@xyz.com, DOJ Tue Jan 08 00:00:00 IST 1985, Salary 10000}

//Sort by Name then Age then dateOfJoining.
Employee{ id 3, name='A', age=10, email A2@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1978, Salary 2000}
Employee{ id 3, name='A', age=10, email A1@xyz.com, DOJ Mon Jan 08 00:00:00 IST 1979, Salary 3000}
Employee{ id 2, name='A', age=40, email A@xyz.com, DOJ Sat Jan 08 00:00:00 IST 1955, Salary 5000}
Employee{ id 4, name='B', age=20, email B@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1989, Salary 2000}
Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Employee{ id 5, name='E', age=50, email E@xyz.com, DOJ Tue Jan 08 00:00:00 IST 1985, Salary 10000}

//Salary > 10000
Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Max salary of employee: Employee{ id 1, name='C', age=30, email C@xyz.com, DOJ Fri Jan 08 00:00:00 IST 1999, Salary 15000}
Min salary of employee: Employee{ id 3, name='A', age=10, email A2@xyz.com, DOJ Sun Jan 08 00:00:00 IST 1978, Salary 2000}

No comments:

Post a Comment