Skip to content

Commit 13cc926

Browse files
committed
feat: implement StreamEmployeeDemo with Java Stream API analytics
Added StreamEmployeeDemo.java demonstrating Java 17 record usage and Stream API operations including grouping, filtering, partitioning, aggregation, and reduction. Outputs real-time employee analytics such as department summaries, salary insights, gender ratios, and experience data. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 100beec commit 13cc926

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package Employee;
2+
3+
import java.util.*;
4+
import java.util.stream.*;
5+
6+
public class StreamEmployeeDemo {
7+
8+
record Employee(int id, String name, int age, String gender,
9+
String department, int yearOfJoining, double salary) {}
10+
11+
public static void main(String[] args) {
12+
13+
List<Employee> emps = new ArrayList<>();
14+
15+
emps.add(new Employee(1, "Jhansi", 32, "Female", "HR", 2011, 25000.0));
16+
emps.add(new Employee(2, "Smith", 25, "Male", "Sales", 2015, 13500.0));
17+
emps.add(new Employee(3, "David", 29, "Male", "Infrastructure", 2012, 18000.0));
18+
emps.add(new Employee(4, "Orlen", 28, "Male", "Development", 2014, 32500.0));
19+
emps.add(new Employee(5, "Charles", 27, "Male", "HR", 2013, 22700.0));
20+
emps.add(new Employee(6, "Cathy", 43, "Male", "Security", 2016, 10500.0));
21+
emps.add(new Employee(7, "Ramesh", 35, "Male", "Finance", 2010, 27000.0));
22+
emps.add(new Employee(8, "Suresh", 31, "Male", "Development", 2015, 34500.0));
23+
emps.add(new Employee(9, "Gita", 24, "Female", "Sales", 2016, 11500.0));
24+
emps.add(new Employee(10, "Mahesh", 38, "Male", "Security", 2015, 11000.5));
25+
emps.add(new Employee(11, "Gouri", 27, "Female", "Infrastructure", 2014, 15700.0));
26+
emps.add(new Employee(12, "Nithin", 25, "Male", "Development", 2016, 28200.0));
27+
emps.add(new Employee(13, "Swathi", 27, "Female", "Finance", 2013, 21300.0));
28+
emps.add(new Employee(14, "Buttler", 24, "Male", "Sales", 2017, 10700.5));
29+
emps.add(new Employee(15, "Ashok", 23, "Male", "Infrastructure", 2018, 12700.0));
30+
emps.add(new Employee(16, "Sanvi", 26, "Female", "Development", 2015, 28900.0));
31+
32+
// 1. Count male and female
33+
Map<String, Long> map1 = emps.stream()
34+
.collect(Collectors.groupingBy(Employee::gender, Collectors.counting()));
35+
System.out.println("\n1️⃣ Male/Female count: " + map1);
36+
37+
// 2. All departments
38+
System.out.println("\n2️⃣ Departments:");
39+
emps.stream().map(Employee::department).distinct().forEach(System.out::println);
40+
41+
// 3. Average age by gender
42+
Map<String, Double> map2 = emps.stream()
43+
.collect(Collectors.groupingBy(Employee::gender, Collectors.averagingInt(Employee::age)));
44+
System.out.println("\n3️⃣ Avg age (M/F): " + map2);
45+
46+
// 4. Highest paid employee
47+
emps.stream()
48+
.max(Comparator.comparingDouble(Employee::salary))
49+
.ifPresent(e -> System.out.println("\n4️⃣ Highest paid: " + e));
50+
51+
// 5. Joined after 2015
52+
System.out.println("\n5️⃣ Joined after 2015:");
53+
emps.stream().filter(e -> e.yearOfJoining() > 2015).map(Employee::name).forEach(System.out::println);
54+
55+
// 6. Employees per department
56+
System.out.println("\n6️⃣ Count per department:");
57+
System.out.println(emps.stream().collect(Collectors.groupingBy(Employee::department, Collectors.counting())));
58+
59+
// 7. Average salary per department
60+
System.out.println("\n7️⃣ Avg salary per department:");
61+
System.out.println(emps.stream().collect(Collectors.groupingBy(Employee::department, Collectors.averagingDouble(Employee::salary))));
62+
63+
// 8. Youngest male in Development
64+
emps.stream()
65+
.filter(e -> e.gender().equals("Male") && e.department().equals("Development"))
66+
.min(Comparator.comparingInt(Employee::age))
67+
.ifPresent(e -> System.out.println("\n8️⃣ Youngest male in Dev: " + e));
68+
69+
// 9. Most experienced employee
70+
emps.stream()
71+
.min(Comparator.comparingInt(Employee::yearOfJoining))
72+
.ifPresent(e -> System.out.println("\n9️⃣ Most experienced: " + e));
73+
74+
// 10. M/F count in Sales
75+
System.out.println("\n🔟 Sales M/F count: " +
76+
emps.stream()
77+
.filter(e -> e.department().equals("Sales"))
78+
.collect(Collectors.groupingBy(Employee::gender, Collectors.counting()))
79+
);
80+
81+
// 11. Avg & total salary
82+
String result = emps.stream()
83+
.collect(Collectors.teeing(
84+
Collectors.averagingDouble(Employee::salary),
85+
Collectors.summingDouble(Employee::salary),
86+
(avg, total) -> "Avg: " + avg + ", Total: " + total
87+
));
88+
System.out.println("\n1️⃣1️⃣ Org salary stats: " + result);
89+
90+
// 12. Partition by age <= 25
91+
Map<Boolean, List<Employee>> partition = emps.stream()
92+
.collect(Collectors.partitioningBy(e -> e.age() <= 25));
93+
System.out.println("\n1️⃣2️⃣ Age <= 25: " + partition.get(true));
94+
System.out.println("Older: " + partition.get(false));
95+
96+
// 13. Oldest employee
97+
emps.stream()
98+
.max(Comparator.comparingInt(Employee::age))
99+
.ifPresentOrElse(
100+
e -> System.out.println("\n1️⃣3️⃣ Oldest employee: " + e),
101+
() -> System.out.println("No employee found.")
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)