public class Employee {
private int id;
private String name;
private double salary;
private String country;
private String designation;
public Employee(int id, String name, double salary, String country, String designation) {
this.id = id;
this.name = name;
this.salary = salary;
this.country = country;
this.designation=designation;
}
//setters and getters
}
public class EmployeeOperationsDemo {
public static void main(String[] args) {
List<Employee> emps = Arrays.asList(new Employee(1, "Suresh", 44000, "India", "Software Engineer"),
new Employee(3, "Clark", 14000, "Aus", "Lead Engineer"),
new Employee(2, "Krishna", 55000, "Aus", "Devops Engineer"),
new Employee(6, "Thanu", 51000, "India", "Software Engineer"),
new Employee(4, "Thanu", 12000, "India", "Lead Engineer"));
}
}
Print the employee names group by designation
emps.stream().collect(Collectors.groupingBy(Employee::getDesignation, Collectors.mapping(Employee::getName, Collectors.toList())))
.entrySet().stream().forEach(entry -> System.out.println(entry.getKey()+" : "+entry.getValue()));
Print the salary group by country
emps.stream().collect(Collectors.groupingBy(e -> e.getCountry(), Collectors.summingDouble(e -> e.getSalary())))
.entrySet()
.stream()
.forEach(entry -> System.out.println(entry.getKey()+" : "+entry.getValue()));
Print the salary group by designation
emps.stream().collect(Collectors.groupingBy(e -> e.getDesignation(), Collectors.summingDouble(e -> e.getSalary())))
.entrySet()
.stream()
.forEach(entry -> System.out.println(entry.getKey()+" : "+entry.getValue()));
Print employee names based on their location
emps.stream().collect(Collectors.groupingBy(e-> e.getCountry(), Collectors.mapping(Employee::getName, Collectors.toList())))
.entrySet()
.stream()
.forEach(entry -> System.out.println(entry.getKey()+" - "+entry.getValue()));
Print the employees whose country is india, if the emp names are matching then compare salary
emps.stream().filter(emp -> emp.getCountry().equalsIgnoreCase("India"))
.sorted(Comparator.comparing(Employee::getName).thenComparingDouble(Employee::getSalary))
.forEach(System.out::println);