Skip to content

Commit 8449d06

Browse files
committed
Add SalaryHistogram class to categorize and print employee salaries into defined ranges
1 parent ef3b832 commit 8449d06

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
package com.superthirty;
3+
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
public class SalaryHistogram {
9+
public static void main(String[] args) {
10+
List<Employee> employees = EmployeeDTO.getEmployees();
11+
//Naive Approach
12+
List<Double> empListUnder30K = employees.stream().map(Employee::getSalary).filter(x -> x < 30000).toList();
13+
List<Double> empListBW30KAnd60K = employees.stream().map(Employee::getSalary)
14+
.filter(x -> x > 30000 && x < 60000).toList();
15+
List<Double> empListMoreThan60K = employees.stream().map(Employee::getSalary)
16+
.filter(x -> x > 60000).toList();
17+
System.out.println(empListUnder30K);
18+
System.out.println(empListBW30KAnd60K);
19+
System.out.println(empListMoreThan60K);
20+
//Pro Approach
21+
Map<String, List<Double>> histogram = employees.stream().map(Employee::getSalary).collect(Collectors.groupingBy(
22+
salary -> {
23+
if (salary < 30000)
24+
return "Under 30K";
25+
if (salary >= 30000 && salary < 60000)
26+
return "Between 30K and 60K";
27+
return "More than or Equal to 60K";
28+
29+
}));
30+
System.out.println(histogram);
31+
}
32+
}

0 commit comments

Comments
 (0)