We will use the below Employee class employeeList as an example to solve the questions.
public class Employee {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary)
{
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String getGender()
{
return gender;
}
public String getDepartment()
{
return department;
}
public int getYearOfJoining()
{
return yearOfJoining;
}
public double getSalary()
{
return salary;
}
@Override
public String toString()
{
return "Id : "+id
+", Name : "+name
+", age : "+age
+", Gender : "+gender
+", Department : "+department
+", Year Of Joining : "+yearOfJoining
+", Salary : "+salary;
}
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0));
employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0));
employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0));
employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0));
employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0));
employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0));
employeeList.add(new Employee(177, "Manu Sharma", 35, "Male", "Account And Finance", 2010, 27000.0));
employeeList.add(new Employee(188, "Wang Liu", 31, "Male", "Product Development", 2015, 34500.0));
employeeList.add(new Employee(199, "Amelia Zoe", 24, "Female", "Sales And Marketing", 2016, 11500.0));
employeeList.add(new Employee(200, "Jaden Dough", 38, "Male", "Security And Transport", 2015, 11000.5));
employeeList.add(new Employee(211, "Jasna Kaur", 27, "Female", "Infrastructure", 2014, 15700.0));
employeeList.add(new Employee(222, "Nitin Joshi", 25, "Male", "Product Development", 2016, 28200.0));
employeeList.add(new Employee(233, "Jyothi Reddy", 27, "Female", "Account And Finance", 2013, 21300.0));
employeeList.add(new Employee(244, "Nicolus Den", 24, "Male", "Sales And Marketing", 2017, 10700.5));
employeeList.add(new Employee(255, "Ali Baig", 23, "Male", "Infrastructure", 2018, 12700.0));
employeeList.add(new Employee(266, "Sanvi Pandey", 26, "Female", "Product Development", 2015, 28900.0));
employeeList.add(new Employee(277, "Anuj Chettiar", 31, "Male", "Product Development", 2012, 35700.0));
}
}
Now, using the above employeeList, let us solve the list of questions.
public static void getCountOfMaleFemale(List<Employee> employeeList) {
Map<String, Long> noOfMaleAndFemaleEmployees=
employeeList.stream()
.collect(Collectors.groupingBy
(Employee::getGender, Collectors.counting()));
System.out.println(noOfMaleAndFemaleEmployees);
}
Output: {Male=11, Female=6}
2. Write a program to print the names of all departments in the organization.
public static void getDepartmentName(List<Employee> employeeList){
employeeList.stream()
.map(Employee::getDepartment)
.distinct()
.forEach(System.out::println);
}
Output:
HR
Sales And Marketing
Infrastructure
Product Development
Security And Transport
Account And Finance
3. Find the average age of Male and Female Employees.
public static void getGender(List<Employee> employeeList) {
Map<String, Double> avgAge = employeeList.stream()
.collect(Collectors.groupingBy
(Employee::getGender,
Collectors.averagingInt
(Employee::getAge)));
System.out.println(avgAge);
}
Output: {Male=30.181818181818183, Female=27.166666666666668}
4. Get the Names of employees who joined after 2015.
public static void getNameOfEmp(List<Employee> employeeList) {
employeeList.stream()
.filter(e -> e.getYearOfJoining() > 2015)
.map(Employee::getName)
.forEach(System.out::println);
}
Output:
Iqbal Hussain
Amelia Zoe
Nitin Joshi
Nicolus Den
Ali Baig
5. Count the number of employees in each department.
public static void countByDept(List<Employee> employeeList) {
Map<String, Long> countByDept = employeeList.stream()
.collect(Collectors.groupingBy
(Employee::getDepartment,
Collectors.counting()));
Set<Entry<String, Long>> entrySet = countByDept.entrySet();
for (Entry<String, Long> entry : entrySet)
{
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
Output:
Product Development : 5
Security And Transport : 2
Sales And Marketing : 3
Infrastructure : 3
HR : 2
Account And Finance : 2
6. Find out the average salary of each department.
public static void avgSalary(List<Employee> employeeList) {
Map<String, Double> avgSalary = employeeList.stream()
.collect(Collectors.groupingBy
(Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)));
Set<Entry<String, Double>> entrySet = avgSalary.entrySet();
for (Entry<String, Double> entry : entrySet)
{
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
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
7. Find out the oldest employee, his/her age and department?
public static void oldestEmp(List<Employee> employeeList) {
Optional<Employee> oldestEmp = employeeList.stream()
.max(Comparator
.comparingInt(Employee::getAge));
Employee oldestEmployee = oldestEmp .get();
System.out.println("Name : "+oldestEmployee.getName());
System.out.println("Age : "+oldestEmployee.getAge());
System.out.println("Department : "+oldestEmployee.getDepartment());
}
Output:
Name : Iqbal Hussain
Age : 43
Department : Security And Transport
8. Find out the average and total salary of the organization.
public static void getEmpSalary(List<Employee> employeeList) {
DoubleSummaryStatistics empSalary = employeeList.stream()
.collect(Collectors
.summarizingDouble(Employee::getSalary));
System.out.println("Average Salary = "+empSalary.getAverage());
System.out.println("Total Salary = "+empSalary.getSum());
}
Output:
Average Salary = 21141.235294117647
Total Salary = 359401.0
9. List down the employees of each department.
public static void listDownDept(List<Employee> employeeList) {
Map<String, List<Employee>> empList = employeeList.stream()
.collect(Collectors
.groupingBy(Employee::getDepartment));
Set<Entry<String, List<Employee>>> entrySet = empList.entrySet();
for (Entry<String, List<Employee>> entry : entrySet)
{
System.out.println("--------------------------------------");
System.out.println("Employees In "+entry.getKey() + " : ");
System.out.println("--------------------------------------");
List<Employee> list = entry.getValue();
for (Employee e : list)
{
System.out.println(e.getName());
}
}
}
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
10. Find out the height of experienced employees in the organization
public static void seniorEmp(List<Employee> employeeList) {
Optional<Employee> seniorEmp = employeeList.stream()
.sorted(Comparator
.comparingInt(Employee::getYearOfJoining)).findFirst();
Employee seniorMostEmployee = seniorEmp.get();
System.out.println("Senior Most Employee Details :");
System.out.println("----------------------------");
System.out.println("ID : "+seniorMostEmployee.getId());
System.out.println("Name : "+seniorMostEmployee.getName());
System.out.println("Age : "+seniorMostEmployee.getAge());
}
Output:
Senior Most Employee Details :
—————————-
ID : 177
Name : Manu Sharma
Age : 35
Thank you for reading the article. Please clap, share and comment. it will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!!!
I Hope this article is helpful, I have Referred Java Concept Of the Day, you can check out the similar other more articles in their website. Thank You!!