Employee Management Quires With Java 8 Features:
![]() |
Employee Management Quires With Java 8 |
Output :
{Male=11, Female=6}
HR
Sales And Marketing
Infrastructure
Product Development
Security And Transport
Account And Finance
Output :
{Male=30.181818181818183, Female=27.166666666666668}
Query 3.4: Get the details of the highest paid employee in the organization?
Use Collectors.maxBy() method which returns maximum element wrapped in an Optional object based on supplied Comparator.
Output :
Details Of Highest Paid Employee :
==================================
ID : 277
Name : Anuj Chettiar
Age : 31
Gender : Male
Department : Product Development
Year Of Joining : 2012
Salary : 35700.0
Query 3.5: Get the names of all employees who have joined after 2015?
For such queries which require filtering of input elements, use Stream.filter() method which filters input elements according to supplied Predicate.
Output :
Iqbal Hussain
Amelia Zoe
Nitin Joshi
Nicolus Den
Ali Baig
Query 3.6: Count the number of employees in each department?
This query is the same as query 3.1 but here we are grouping the elements by the department.
Output :
Product Development : 5
Security And Transport : 2
Sales And Marketing : 3
Infrastructure : 3
HR : 2
Account And Finance : 2
Query 3.7: What is the average salary of each department?
Use the same method as in the above query 3.6, but here pass Collectors.averagingDouble(Employee::getSalary) as second argument to Collectors.groupingBy() method.
Output :
Product Development : 31960.0
Security And Transport : 10750.25
Sales And Marketing : 11900.166666666666
Infrastructure : 15466.666666666666
HR : 23850.0
Account And Finance : 24150.0
Query 3.8: Get the details of the youngest male employee in the product development department?
For this query, use Stream.filter() method to filter male employees in the product development department and to find the youngest among them, use Stream.min() method.
Output :
Details Of Youngest Male Employee In Product Development :
———————————————-
ID : 222
Name : Nitin Joshi
Age : 25
Year Of Joinging : 2016
Salary : 28200.0
Query 3.9: Who has the most working experience in the organization?
For this query, sort employeeList by yearOfJoining in the natural order, and the first employee will have the most working experience in the organization. To solve this query, we will be using sorted() and findFirst() methods of Stream.
Output :
Senior Most Employee Details :
—————————-
ID : 177
Name : Manu Sharma
Age : 35
Gender : Male
Age : Account And Finance
Year Of Joinging : 2010
Salary : 27000.0
Query 3.10: How many male and female employees are there in the sales and marketing team?
This query is the same as query 3.1, but here use the filter() method to filter sales and marketing employees.
Output :
{Female=1, Male=2}
Query 3.11: What is the average salary of male and female employees?
This query is the same as query 3.3 where you have found the average age of male and female employees. Here, we will be finding the average salary of male and female employees.
Output :
{Male=21300.090909090908, Female=20850.0}
Query 3.12: List down the names of all employees in each department?
For this query, we will be using Collectors.groupingBy() method by passing Employee::getDepartment as an argument.
Output :
————————————–
Employees In Product Development :
————————————–
Murali Gowda
Wang Liu
Nitin Joshi
Sanvi Pandey
Anuj Chettiar
————————————–
Employees In Security And Transport :
————————————–
Iqbal Hussain
Jaden Dough
————————————–
Employees In Sales And Marketing :
————————————–
Paul Niksui
Amelia Zoe
Nicolus Den
————————————–
Employees In Infrastructure :
————————————–
Martin Theron
Jasna Kaur
Ali Baig
————————————–
Employees In HR :
————————————–
Jiya Brein
Nima Roy
————————————–
Employees In Account And Finance :
————————————–
Manu Sharma
Jyothi Reddy
Query 3.13: What are the average salary and total salary of the whole organization?
For this query, we use Collectors.summarizingDouble() on Employee::getSalary which will return statistics of the employee salary like max, min, average and total.
Output :
Average Salary = 21141.235294117647
Total Salary = 359401.0
Query 3.14: Separate the employees who are younger or equal to 25 years from those employees who are older than 25 years.
For this query, we will be using Collectors.partitioningBy() method which separates input elements based on supplied Predicate.
Output :
—————————-
Employees younger than or equal to 25 years :
—————————-
Paul Niksui
Amelia Zoe
Nitin Joshi
Nicolus Den
Ali Baig
—————————-
Employees older than 25 years :
—————————-
Jiya Brein
Martin Theron
Murali Gowda
Nima Roy
Iqbal Hussain
Manu Sharma
Wang Liu
Jaden Dough
Jasna Kaur
Jyothi Reddy
Sanvi Pandey
Anuj Chettiar
Query 3.15: Who is the oldest employee in the organization? What is his age and which department does he belong to?
Output :
Name : Iqbal Hussain
Age : 43
Department : Security And Transport
0 Comments