1+ package demoApplication ;
2+
3+ import java .util .List ;
4+ import java .util .Arrays ;
5+ import java .util .Map ;
6+ import java .util .Set ;
7+ import java .util .stream .Collectors ;
8+
9+ public class DemoApplication {
10+
11+ static List <Employee > employeeList = Arrays .asList (
12+
13+ new Employee (1 ,"Abraham" ,32 ,"IT" ,"Mumbai" ,20000 ,"Male" ),
14+ new Employee (2 ,"Mary" ,21 ,"Sales" ,"Chennai" ,25000 ,"Female" ),
15+ new Employee (3 ,"Joe" ,32 ,"IT" ,"Chennai" ,22000 ,"Male" ),
16+ new Employee (4 ,"John" ,27 ,"Sales" ,"Gurgaon" ,29000 ,"Male" ),
17+ new Employee (5 ,"Liza" ,54 ,"Sales" ,"Bangalore" ,32000 ,"Female" ),
18+ new Employee (6 ,"Peter" ,33 ,"Admin" ,"Mumbai" ,31500 ,"Male" ),
19+ new Employee (7 ,"Harry" ,35 ,"Research" ,"Kochi" ,21000 ,"Male" ),
20+ new Employee (5 ,"Romit" ,25 ,"Research" ,"Kanpur" ,21000 ,"Male" )
21+ );
22+
23+ public static void main (String [] args ) {
24+ System .out .println (" Here are the solution of the 15 questions : " );
25+
26+ //1.Find the list of employees whose name starts with "A".
27+
28+ List <Employee > employeeWhoseNameStartsWithA = employeeList .stream ().filter (employee -> employee .getName ().startsWith ("A" )).collect (Collectors .toList ());
29+
30+ System .out .println ("Employees whose name starts with A \n " +employeeWhoseNameStartsWithA );
31+
32+
33+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
34+
35+
36+ //2.Group the employees by Department names.
37+
38+ Map <String , List <Employee >> employeesWithDeptName = employeeList .stream ().collect (Collectors .groupingBy (Employee ::getDapartName ));
39+
40+ System .out .println ("List of employees with department names " );
41+ employeesWithDeptName .forEach ((dept ,empList ) -> {
42+ System .out .println (dept );
43+ empList .forEach (emp -> {
44+ System .out .println (emp .getId ());;
45+ System .out .println (emp .getName ());;
46+ System .out .println (emp .getAge ());;
47+ System .out .println (emp .getDapartName ());;
48+ System .out .println ( emp .getAddress ());;
49+ System .out .println (emp .getSalary ());
50+ System .out .println (emp .getGender ());
51+ });
52+
53+ System .out .println ("-----------------------------------------------------------------------------" );
54+ });
55+
56+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
57+
58+
59+ //3.Find the total count of employees using stream.
60+
61+ long count = employeeList .stream ().count ();
62+ System .out .println ("Total number of employees : " + count );
63+
64+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
65+
66+ //4.Find the max age of employees.
67+
68+ int maxAge = employeeList .stream ().mapToInt (Employee ::getAge ).max ().getAsInt ();
69+ System .out .println ("Max Age : " + maxAge );
70+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
71+
72+ //5.Find all department names.
73+ Set <String > allDeptNames = employeeList .stream ().map (Employee ::getDapartName ).collect (Collectors .toSet ());
74+ System .out .println ("All department names : " +allDeptNames );
75+
76+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
77+ //6.Find the count of employees in each department.
78+ Map <String , Long > employeeCountByDept = employeeList .stream ().collect (Collectors .groupingBy (Employee ::getDapartName , Collectors .counting ()));
79+ System .out .println ("Employee count by department" );
80+ employeeCountByDept .forEach ((dept ,value ) ->{
81+ System .out .println (dept +" : " + value );
82+ } );
83+
84+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
85+ //7.Find the list of employees whose age is less than 30.
86+ Set <Employee > ageLessThan30 = employeeList .stream ().filter (employee -> employee .getAge () < 30 ).collect (Collectors .toSet ());
87+
88+ System .out .println ("Employees having age less than 30" +ageLessThan30 );
89+
90+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
91+ //8.Find the list of employees whose age is between 26 and 31
92+
93+ List <Employee > ageBetween26and31 = employeeList .stream ().filter (employee -> employee .getAge () < 31 && employee .getAge () > 26 ).collect (Collectors .toList ());
94+ System .out .println ("Employees having age between 26 and 31" +ageBetween26and31 );
95+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
96+ //9.Find the average age of male and female employees.
97+ Map <String , Double > avgAgeOfMaleAndFemale = employeeList .stream ().collect (Collectors .groupingBy (Employee ::getGender , Collectors .averagingDouble (Employee ::getAge )));
98+ System .out .println ("Average age of Male and Female employees :" );
99+ avgAgeOfMaleAndFemale .forEach ((gender ,age ) ->{
100+ System .out .println (gender +" : " + age );
101+ });
102+
103+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
104+ //10.Find the department who is having the maximum number of employees.
105+ Map .Entry <String , Long > deptMaxEmp = employeeList .stream ().collect (Collectors .groupingBy (Employee ::getDapartName , Collectors .counting ())).entrySet ().stream ().max (Map .Entry .comparingByValue ()).get ();
106+
107+ System .out .println ("Department having maximum number of employees is \n " + deptMaxEmp .getKey () +" \n and the employees number is " +deptMaxEmp .getValue ());
108+
109+
110+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
111+ //11.Find the Employee who stays in Delhi and sort them by their names.
112+
113+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
114+
115+ //12.Find the average salary in all department.
116+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
117+ //13.Find the highest salary in each department.
118+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
119+ //14.Find the list of employee and sort them by their salary.
120+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
121+ //15.Find the employee who as second highest salary.
122+ System .out .println ("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
123+
124+
125+
126+ }
127+ }
0 commit comments