Programming Java 8
Programming Java 8
System.out.println("Lowest
Percentage :
"+studentStats.getMin());
System.out.println("Average
Percentage :
"+studentStats.getAverage())
;
52) How do you get total number of students from the
given list of students?
1 Long studentCount =
studentList.stream().collect(C
ollectors.counting());
53) How do you get the students grouped by subject
from the given list of students?
1 Map<String, List<Student>>
studentsGroupedBySubject =
studentList.stream().collect(C
ollectors.groupingBy(Student:
:getSubject));
Questions from 54 to 61 are on the following Employee class.
1 class Employee
2{
3 int id;
4
5 String name;
6
7 int age;
8
9 String gender;
10
11 String department;
12
13 int yearOfJoining;
14
15 double salary;
16
17 public Employee(int id,
18 String name, int age, String
19 gender, String department,
20 int yearOfJoining, double
21 salary)
22 {
23 this.id = id;
24 this.name = name;
25 this.age = age;
26 this.gender = gender;
27 this.department =
28 department;
29 this.yearOfJoining =
30 yearOfJoining;
31 this.salary = salary;
32 }
33
34 public int getId()
35 {
36 return id;
37 }
38
39 public String getName()
40 {
41 return name;
42 }
43
44 public int getAge()
45 {
46 return age;
47 }
48
49 public String getGender()
50 {
51 return gender;
52 }
53
54 public String
55 getDepartment()
56 {
57 return department;
58 }
59
60 public int
61 getYearOfJoining()
62 {
63 return yearOfJoining;
64 }
65
66 public double getSalary()
67 {
68 return salary;
69 }
70
71 @Override
72 public String toString()
73 {
74 return "Id : "+id
+", Name :
"+name
+", age : "+age
+", Gender :
"+gender
+", Department :
"+department
+", Year Of
Joining : "+yearOfJoining
+", Salary :
"+salary;
}
}
54) Given a list of employees, write a Java 8 code to
count the number of employees in each department?
1 Map<String, Long>
2 employeeCountByDepartmen
t=
employeeList.stream().collect
(Collectors.groupingBy(Emplo
yee::getDepartment,
Collectors.counting()));
55) Given a list of employees, find out the average
salary of male and female employees?
1 Map<String, Double>
2 avgSalaryOfMaleAndFemaleE
mployees=
employeeList.strea
m().collect(Collectors.groupin
gBy(Employee::getGender,
Collectors.averagingDouble(E
mployee::getSalary)));
56) Write a Java 8 code to get the details of highest paid
employee in the organization from the given list of
employees?
1 Optional<Employee>
2 highestPaidEmployeeWrappe
r=
employeeList.stream().collect
(Collectors.maxBy(Comparat
or.comparingDouble(Employe
e::getSalary)));
57) Write the Java 8 code to get the average age of each
department in an organization?
1 Map<String, Double>
2 avgAgeOfEachDepartment =
employeeList.strea
m().collect(Collectors.groupin
gBy(Employee::getDepartme
nt,
Collectors.averagingInt(Empl
oyee::getAge)));
58) Given a list of employees, how do you find out who
is the senior most employee in the organization?
1 Optional<Employee>
2 seniorMostEmployeeWrapper
=
employeeList.stream().sorted
(Comparator.comparingInt(E
mployee::getYearOfJoining)).
findFirst();
59) Given a list of employees, get the details of the
most youngest employee in the organization?
1 Optional<Employee>
2 youngestEmployee =
employeeList.strea
m().min(Comparator.compari
ngInt(Employee::getAge));
60) How do you get the number of employees in each
department if you have given a list of employees?
1 Map<String, Long>
2 employeeCountByDepartmen
t=
employeeList.stream().collect
(Collectors.groupingBy(Emplo
yee::getDepartment,
Collectors.counting()));
61) Given a list of employees, find out the number of
male and female employees in the organization?
1 Map<String, Long>
2 noOfMaleAndFemaleEmploye
es=
employeeList.stream().collect
(Collectors.groupingBy(Emplo
yee::getGender,
Collectors.counting()));
See More : Solving Real Time Queries Using Java 8
Features -Employee Management System
62) What will be the output of the following statement?
1 System.out.println(IntStream.
range(0, 5).sum());