forked from Rapter1990/JavaStreamAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeExampleV1.java
More file actions
105 lines (73 loc) · 4.79 KB
/
EmployeeExampleV1.java
File metadata and controls
105 lines (73 loc) · 4.79 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package employeeexample;
import java.util.*;
import java.util.stream.Collectors;
public class EmployeeExampleV1 {
public static void main(String[] args) {
List<Employee> employeeList = EmployeeLoadData.loadEmployee();
// Exercise 1 : How many male and female employees are there in the organization?
howManyMaleAndFemaleInTheOrganization(employeeList);
// Exercise 2 : Print the name of all departments in the organization.
printNameOfAllDepartmentsInTheOrganization(employeeList);
// Exercise 3 : What is the average age of male and female employees?
averageAgeOfAllMaleAndFemaleInTheOrganization(employeeList);
// Exercise 4 : Get the details of highest paid employee in the organization
getDetailsOfHighestReleasedEmployee(employeeList);
// Exercise 5 : Get the names of all employees who have joined after 2015
getNamesOfEmployeesWhoHaveJoinedAfter2015(employeeList);
// Exercise 6 : Count the number of employees in each department
countNumberEmployeeInEachDepartment(employeeList);
// Exercise 7 : What is the average salary of each department?
averageSalaryOfEachDepartment(employeeList);
// Exercise 8 : Who has the most working experience in the organization?
whoHasMostWorkingExperience(employeeList);
// Exercise 9 : Get the details of youngest male employee in the each department.
getDetailsOfYoungestMaleInEachDepartment(employeeList);
}
// Ex:1
private static void howManyMaleAndFemaleInTheOrganization(List<Employee> employeeList) {
Map<String, Long> maleAndFemaleEmployeesCount = employeeList.stream().collect(Collectors.groupingBy(e -> e.getEmp_gender(), Collectors.counting()));
System.out.println("--> howManyMaleAndFemaleInTheOrganization:");
maleAndFemaleEmployeesCount.entrySet().forEach(e -> System.out.println(e.getKey() + ", " + e.getValue()));
}
// Ex:2
private static void printNameOfAllDepartmentsInTheOrganization(List<Employee> employeeList) {
Set<String> allDepts = employeeList.stream().map(e -> e.getEmp_dept()).collect(Collectors.toSet());
System.out.println("--> printNameOfAllDepartmentsInTheOrganization: \n" + allDepts);
}
// Ex:3
private static void averageAgeOfAllMaleAndFemaleInTheOrganization(List<Employee> employeeList) {
Map<String, Double> avgAgeOfMFEmployees = employeeList.stream().collect(Collectors.groupingBy(e -> e.getEmp_gender(), Collectors.averagingInt(e -> e.getEmp_age())));
System.out.println("--> averageAgeOfAllMaleAndFemaleInTheOrganization:");
avgAgeOfMFEmployees.entrySet().forEach(e -> System.out.println(e.getKey() + ", " + e.getValue()));
}
// Ex:4
private static void getDetailsOfHighestReleasedEmployee(List<Employee> employeeList) {
Optional<Employee> highPaidEmployee = employeeList.stream().collect(Collectors.maxBy(Comparator.comparing(e -> e.getEmp_salary())));
System.out.println("--> getDetailsOfHighestReleasedEmployee: \n" + highPaidEmployee);
}
// Ex: 5
private static void getNamesOfEmployeesWhoHaveJoinedAfter2015(List<Employee> employeeList) {
List<Employee> employees = employeeList.stream().filter(e -> e.getEmp_doj() > 2015).collect(Collectors.toList());
System.out.println("--> getNamesOfEmployeesWhoHaveJoinedAfter2015: \n" + employees);
}
// Ex: 6
private static void countNumberEmployeeInEachDepartment(List<Employee> employeeList) {
Map<String, Long> employeeInDept = employeeList.stream().collect(Collectors.groupingBy(Employee::getEmp_dept, Collectors.counting()));
System.out.println("--> countNumberEmployeeInEachDepartment: \n" + employeeInDept);
}
// Ex: 7
private static void averageSalaryOfEachDepartment(List<Employee> employeeList) {
Map<String, Double> avgSalByDept = employeeList.stream().collect(Collectors.groupingBy(Employee::getEmp_dept, Collectors.averagingDouble(Employee::getEmp_salary)));
System.out.println("--> averageSalaryOfEachDepartment: \n" + avgSalByDept);
}
// Ex: 8
private static void whoHasMostWorkingExperience(List<Employee> employeeList) {
Optional<Employee> employee = employeeList.stream().min(Comparator.comparing(Employee::getEmp_doj));
System.out.println("--> whoHasMostWorkingExperience: \n" + employee);
}
// Ex: 9
private static void getDetailsOfYoungestMaleInEachDepartment(List<Employee> employeeList) {
Map<String, Optional<Employee>> map = employeeList.stream().filter(e -> e.getEmp_gender().equals("Male")).collect(Collectors.groupingBy(Employee::getEmp_dept, Collectors.minBy(Comparator.comparing(Employee::getEmp_age))));
System.out.println("--> getDetailsOfYoungestMaleInEachDepartment: \n" + map);
}
}