Top Java 8 Stream API Interview Questions (Part 2)

Sandip SamagSandip Samag
10 min read
  1. Given a list of employees, find the employee with the highest salary.
package com.streams.maxmin;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeMaxSalaryExample {

    private static Employee getMaxSalaryEmployee(List<Employee> empList){
        return empList.stream()
            .collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary))).get();
    }

    private static Employee getMaxSalEmployee(List<Employee> empList){
        return empList.stream()
        .max(Comparator.comparing(Employee::getSalary)).get();
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        System.out.println("===========================");
        System.out.println("Max Salary Employee details: ");
        System.out.println("===========================");
        System.out.println(getMaxSalaryEmployee(unsortedList));
        }
}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Max Salary Employee details: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
  1. Given a list of employees, find the employee with the lowest salary.
package com.streams.maxmin;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeMinSalaryExample {


    private static Employee getMinSalaryEmployee(List<Employee> empList){
        return empList.stream()
            .collect(Collectors.minBy(Comparator.comparing(Employee::getSalary))).get();
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        System.out.println("===========================");
        System.out.println("Min Salary Employee details: ");
        System.out.println("===========================");
        System.out.println(getMinSalaryEmployee(unsortedList));

    }

}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Min Salary Employee details: 
===========================
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
  1. Given a list of employees, find the oldest employee within each department.
package com.streams.maxmin;

import java.util.Comparator;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class OldestEmployeeDeptWiseExample {

    public static void main(String[] args) {

        Map<String, Employee> empMap = EmployeeDatabase.getEmployees().stream()
                .collect(Collectors.groupingBy(Employee::getDept,
                        Collectors.collectingAndThen(
                                Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Employee::getAge))),
                                Optional::get)));

        empMap.entrySet().stream()
                .forEach(entry -> System.out.println("Gender: " + entry.getKey() + "," + " Age: " + entry.getValue()));

    }

}

Output

Gender: Sales, Age: Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Gender: Infrastructure, Age: Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Gender: HR, Age: Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Gender: IT, Age: Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
  1. Given a list of employees, find the average age for each gender.
package com.streams.sumavg;

import java.util.Map;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class GenderWiseAvgAgeExample {

    public static void main(String[] args) {

        Map<String,Double> empMap = EmployeeDatabase.getEmployees().stream()
                                        .collect(Collectors.groupingBy(Employee::getGender,
                                                Collectors.averagingInt(Employee::getAge)));

        empMap.entrySet().stream().forEach(entry -> System.out.println("Gender: " + entry.getKey() + "," +
                " Age: " + entry.getValue()));

    }

}

Output

Gender: Female, Age: 30.333333333333332
Gender: Male, Age: 40.0
  1. Given a list of employees, sort the employees in ascending order of salary.
package com.streams.sort;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeAscendingSortExample {

    private static List<Employee> sortBySalary(List<Employee> empList){
        List<Employee> sortedList = empList.stream()
                .sorted(Comparator.comparing(Employee::getSalary))
                .collect(Collectors.toList());
        return sortedList;
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        List<Employee> sortedList =sortBySalary(unsortedList);
        System.out.println("===========================");
        System.out.println("Employee list after sorting: " );
        System.out.println("===========================");
        sortedList.forEach(System.out::println);

    }

}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Employee list after sorting: 
===========================
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
  1. Given a list of employees, sort the employees in descending order of salary.

package com.streams.sort;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeDescendingSortExample {

    private static List<Employee> sortBySalary(List<Employee> empList){
        List<Employee> sortedList = empList.stream()
                .sorted(Comparator.comparing(Employee::getSalary).reversed())
                .collect(Collectors.toList());
        return sortedList;
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        List<Employee> sortedList =sortBySalary(unsortedList);
        System.out.println("===========================");
        System.out.println("Employee list after sorting: " );
        System.out.println("===========================");
        sortedList.forEach(System.out::println);

    }

}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Employee list after sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
  1. Given a list of employees, sort the employees by age and salary.

package com.streams.sort;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeSortByAgeAndSalaryExample {

    private static List<Employee> sortBySalary(List<Employee> empList){
        Comparator<Employee> ageComparator = Comparator.comparing(Employee::getAge);
        Comparator<Employee> salaryComparator = Comparator.comparing(Employee::getSalary);

        List<Employee> sortedList = empList.stream()
                .sorted(ageComparator.thenComparing(salaryComparator))
                .collect(Collectors.toList());
        return sortedList;
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        List<Employee> sortedList =sortBySalary(unsortedList);
        System.out.println("===========================");
        System.out.println("Employee list after sorting: " );
        System.out.println("===========================");
        sortedList.forEach(System.out::println);

    }

}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Employee list after sorting: 
===========================
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
  1. Given a list of employees, find any female employee whose salary is greater than 40000.

package com.streams.find;

import java.util.List;
import java.util.Optional;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeFindAnyExample {

    private static Optional<Employee> findAny(List<Employee> empList){
        return empList.stream()
                .filter(e -> e.getSalary() > 40000l && e.getGender() == "Female")
                .findAny();
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        System.out.println("===========================");
        System.out.println("Employee details: ");
        System.out.println("===========================");
        System.out.println(findAny(unsortedList));

    }

}

Output

===========================
Employee list: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Employee details: 
===========================
Optional[Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]]
  1. Given a list of employees, find the first male employee.
package com.streams.find;

import java.util.List;
import java.util.Optional;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeFindFirstExample {

    private static Optional<Employee> findFirst(List<Employee> empList){
        return empList.stream()
                .filter(e -> e.getGender() =="Male")
                .findFirst();
    }

    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list: ");
        System.out.println("===========================");
        unsortedList.forEach(System.out::println);
        System.out.println("===========================");
        System.out.println("Employee details: ");
        System.out.println("===========================");
        System.out.println(findFirst(unsortedList));

    }


}

Output

===========================
Employee list: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Employee details: 
===========================
Optional[Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]]
  1. Given a list of employees, find the maximum, minimum, average, etc. salary.
package com.streams.sumavg;

import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;

import com.model.Employee;
import com.model.EmployeeDatabase;

public class EmployeeSummaryStatisticsExample {

    private static LongSummaryStatistics getSummaryStatistics(List<Employee> empList){
        LongSummaryStatistics longSummaryStatistics = empList.stream()
            .collect(Collectors.summarizingLong(Employee::getSalary));
        return longSummaryStatistics;
    }


    public static void main(String[] args) {
        List<Employee> unsortedList = EmployeeDatabase.getEmployees();
        System.out.println("===========================");
        System.out.println("Employee list before sorting: ");
        System.out.println("===========================");
        LongSummaryStatistics longSummaryStatistics = getSummaryStatistics(unsortedList);
        unsortedList.forEach(System.out::println);
        System.out.println("===========================");
        System.out.println("Max Salary: " + longSummaryStatistics.getMax());
        System.out.println("Min Salary: " + longSummaryStatistics.getMin());
        System.out.println("Sum of all Salaries: " + longSummaryStatistics.getSum());
        System.out.println("Average of all Salaries: " + longSummaryStatistics.getAverage());
        System.out.println("Count of all Salaries: " + longSummaryStatistics.getCount());
        System.out.println("===========================");

    }



}

Output

===========================
Employee list before sorting: 
===========================
Employee [id=1, name=Sandip, age=40, salary=600000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=2, name=Sachin, age=35, salary=1000, gender=Male, dept=HR, skills=[Java, Spring, Sql]]
Employee [id=3, name=Saurabh, age=25, salary=2000, gender=Male, dept=Sales, skills=[Sql, Mainframe]]
Employee [id=4, name=Kamalakshi, age=30, salary=40000, gender=Female, dept=IT, skills=[Sql, Mainframe]]
Employee [id=5, name=Ann, age=37, salary=45000, gender=Female, dept=IT, skills=[.Net, C,C++]]
Employee [id=6, name=Geeta, age=24, salary=60000, gender=Female, dept=HR, skills=[Sql, Angular, Java Script]]
Employee [id=7, name=Rahul, age=39, salary=50000, gender=Male, dept=Infrastructure, skills=[React, Java Script, Node.js]]
Employee [id=8, name=James, age=29, salary=400000, gender=Male, dept=Infrastructure, skills=[.Net, C,C++]]
Employee [id=9, name=Dwayne, age=53, salary=420000, gender=Male, dept=IT, skills=[Java, Spring, Sql, Microservices]]
Employee [id=10, name=Alex, age=59, salary=600000, gender=Male, dept=IT, skills=[Sql, Angular, Java Script]]
===========================
Max Salary: 600000
Min Salary: 1000
Sum of all Salaries: 2218000
Average of all Salaries: 221800.0
Count of all Salaries: 10
===========================
1
Subscribe to my newsletter

Read articles from Sandip Samag directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandip Samag
Sandip Samag

I am experienced Java Technical Lead with a proven track record of leading and mentoring development teams. Proficient in Java programming, frameworks like Spring, Spring Boot, and Microservices. Skilled in project planning, execution, and agile methodologies. Strong communicator with a focus on delivering high-quality, scalable software solutions.