Skip to content

Commit 7b8a56c

Browse files
committed
add stream solution
1 parent ab138c1 commit 7b8a56c

File tree

1 file changed

+105
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy