File tree Expand file tree Collapse file tree
src/main/java/com/superthirty Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments