0% found this document useful (0 votes)
6 views

Programming Java 8

Uploaded by

shiv04dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Programming Java 8

Uploaded by

shiv04dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

44) What is the difference between Java 8 StringJoiner,

String.join() and Collectors.joining()?


StringJoiner is a class in java.util package which internally
uses StringBuilder class to join the strings. Using StringJoiner, you
can join only the strings, but not the array of strings or list of
strings.
String.join() method internally uses StringJoiner class. This
method can be used to join strings or array of strings or list of
strings, but only with delimiter not with prefix and suffix.
Collectors.joining() method can also be used to join strings or
array of strings or list of strings with delimiter and it also
supports prefix and suffix.
See More : Java 8 StringJoiner, String.join() And
Collectors.joining()
45) Name three important classes of Java 8 Date and
Time API?
java.time.LocalDate, java.time.LocalTime and java.time.LocalDateTime
46) How do you get current date and time using Java 8
features?
1 LocalDateTime
currentDateTime =
LocalDateTime.now();
Questions from 47 to 53 depends on the following Student class.
1 class Student
2{
3 String name;
4
5 int id;
6
7 String subject;
8
9 double percentage;
10
11 public Student(String
12 name, int id, String subject,
13 double percentage)
14 {
15 this.name = name;
16 this.id = id;
17 this.subject = subject;
18 this.percentage =
19 percentage;
20 }
21
22 public String getName()
23 {
24 return name;
25 }
26
27 public int getId()
28 {
29 return id;
30 }
31
32 public String getSubject()
33 {
34 return subject;
35 }
36
37 public double
38 getPercentage()
39 {
40 return percentage;
41 }
42
43 @Override
44 public String toString()
{
return
name+"-"+id+"-"+subject+"-
"+percentage;
}
}
47) Given a list of students, write a Java 8 code to
partition the students who got above 60% from those
who didn’t?
1 Map<Boolean,
List<Student>>
studentspartionedByPercenta
ge =
studentList.stream().collect(C
ollectors.partitioningBy(stude
nt ->
student.getPercentage() >
60.0));
48) Given a list of students, write a Java 8 code to get
the names of top 3 performing students?
1 List<Student> top3Students
=
studentList.stream().sorted(C
omparator.comparingDouble(
Student::getPercentage).reve
rsed()).limit(3).collect(Collect
ors.toList());
49) Given a list of students, how do you get the name
and percentage of each student?
1 Map<String, Double>
namePercentageMap =
studentList.stream().collect(C
ollectors.toMap(Student::get
Name,
Student::getPercentage));
50) Given a list of students, how do you get the subjects
offered in the college?
1 Set<String> subjects =
studentList.stream().map(Stu
dent::getSubject).collect(Coll
ectors.toSet());
51) Given a list of students, write a Java 8 code to get
highest, lowest and average percentage of students?
1 DoubleSummaryStatistics
2 studentStats =
3 studentList.stream().collect(C
4 ollectors.summarizingDouble(
5 Student::getPercentage));
6
7 System.out.println("Highest
Percentage :
"+studentStats.getMax());

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());

You might also like