9- Sorting using stream and generating output in array and List format
Write a code for employee which contains information like id , name , department and salary and sort in ascending or descending order on the basis of any information like salary, department
Employee
package com.java.stream.api.example;
public class Employee {
private int id;
private String name;
private String dept;
private long salary;
//Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public Employee(int id, String name, String dept, long salary) {
super();
this.id = id;
this.name = name;
this.dept = dept;
this.salary = salary;
}
public Employee() {
super();
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + ", salary=" + salary + "]";
}
}
Database
package com.java.stream.api.example;
import java.util.ArrayList;
import java.util.List;
public class DataBase {
public static List<Employee> getEmployees() {
List<Employee> list = new ArrayList<>();
list.add(new Employee(176, "Roshan", "IT", 600000));
list.add(new Employee(388, "Bikash", "CIVIL", 900000));
list.add(new Employee(470, "Bimal", "DEFENCE", 500000));
list.add(new Employee(624, "Sourav", "CORE", 400000));
list.add(new Employee(176, "Prakash", "SOCIAL", 1200000));
return list;
}
}
SortListDemo
package com.java.stream.api.example;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortListDemo {
public static void main(String[] args) {
List<Employee> employees = DataBase.getEmployees();
// Sorting salary in Ascending order of salary in array output
Collections.sort(employees, (o1, o2) -> (int) (o1.getSalary() - o2.getSalary()));
System.out.println("Ascending order of salary: "+employees);
System.out.println("----------------");
// Sorting salary in Descending order of salary in array output
Collections.sort(employees, (o1, o2) -> (int) (o2.getSalary() - o1.getSalary()));
System.out.println("Descending order of salary: "+employees);
System.out.println("----------------");
// Sorting salary in ascending order of salary in List Output
employees.stream().sorted(Comparator.comparing(emp -> emp.getSalary())).forEach(System.out::println);
System.out.println("----------------");
// Sorting salary in ascending order of departmentName in List Output
employees.stream().sorted(Comparator.comparing(Employee::getDept)).forEach(System.out::println);
}
}
CONSOLE
Ascending order of salary: [Employee [id=624, name=Sourav, dept=CORE, salary=400000], Employee [id=470, name=Bimal, dept=DEFENCE, salary=500000], Employee [id=176, name=Roshan, dept=IT, salary=600000], Employee [id=388, name=Bikash, dept=CIVIL, salary=900000], Employee [id=176, name=Prakash, dept=SOCIAL, salary=1200000]]
----------------
Descending order of salary: [Employee [id=176, name=Prakash, dept=SOCIAL, salary=1200000], Employee [id=388, name=Bikash, dept=CIVIL, salary=900000], Employee [id=176, name=Roshan, dept=IT, salary=600000], Employee [id=470, name=Bimal, dept=DEFENCE, salary=500000], Employee [id=624, name=Sourav, dept=CORE, salary=400000]]
----------------
Employee [id=624, name=Sourav, dept=CORE, salary=400000]
Employee [id=470, name=Bimal, dept=DEFENCE, salary=500000]
Employee [id=176, name=Roshan, dept=IT, salary=600000]
Employee [id=388, name=Bikash, dept=CIVIL, salary=900000]
Employee [id=176, name=Prakash, dept=SOCIAL, salary=1200000]
----------------
Employee [id=388, name=Bikash, dept=CIVIL, salary=900000]
Employee [id=624, name=Sourav, dept=CORE, salary=400000]
Employee [id=470, name=Bimal, dept=DEFENCE, salary=500000]
Employee [id=176, name=Roshan, dept=IT, salary=600000]
Employee [id=176, name=Prakash, dept=SOCIAL, salary=1200000]
-------------------------------------------------------------------------------------------------------------------------------------------------
Add extra code in which you filter the database on basis of salary
package com.java.stream.api.example;
import java.util.List;
import java.util.stream.Collectors;
public class TaxService{
public static List<Employee> evaluateTaxUsers(String input) {
// condition(true or false) ? (expression for true) : (expression for false)
//basically if condition comes true 1st expression runs(After question mark) ,Otherwise 2nd expression(After Semicolon)
return (input.equalsIgnoreCase("tax"))
? DataBase.getEmployees().stream().filter(emp -> emp.getSalary() > 500000).collect(Collectors.toList())
: DataBase.getEmployees().stream().filter(emp -> emp.getSalary() <= 500000)
.collect(Collectors.toList());
}
public static void main(String[] args) {
//Print true Case(after question mark)
System.out.println(evaluateTaxUsers("tax"));
System.out.println("--------------");
//Print false Case(after semicolon)
System.out.println(evaluateTaxUsers("taxes"));
}
}
CONSOLE
[Employee [id=176, name=Roshan, dept=IT, salary=600000], Employee [id=388, name=Bikash, dept=CIVIL, salary=900000], Employee [id=176, name=Prakash, dept=SOCIAL, salary=1200000]]
--------------
[Employee [id=470, name=Bimal, dept=DEFENCE, salary=500000], Employee [id=624, name=Sourav, dept=CORE, salary=400000]]
Comments
Post a Comment