-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
27 lines (20 loc) · 976 Bytes
/
Employee.java
File metadata and controls
27 lines (20 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.interview.java8;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public record Employee(String name, String department, Double salary) {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(new Employee("Ramesh", "IT", 750000.0),
new Employee("Suresh", "HR", 450000.0), new Employee("Vishal", "Account", 55000.0),
new Employee("Mohit", "HR", 850000.0), new Employee("Sachin", "IT", 40000.0)
);
//1. Group by department
Map<String, List<Employee>> grpByDept = employees.stream()
.collect(Collectors.groupingBy(emp->emp.department,Collectors.filtering(e->e.salary>50000, Collectors.toList())));
//2. high earn name
Map<String, List<String>> highEarnNames = employees.stream()
.collect(Collectors.groupingBy(emp->emp.department,
Collectors.filtering(emp->emp.salary>50000, Collectors.mapping(emp->emp.name, Collectors.toList()))));
}
}