Comparision of Employee Operations with Stream and Non-Stream

First, let’s define the Employee class:

public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    // constructors, getters and setters
}

Now, let’s create a list of employees:

List<Employee> employees = Arrays.asList(
    new Employee("John", 30, 5000.0),
    new Employee("Jane", 25, 7000.0),
    new Employee("Tom", 35, 9000.0),
    new Employee("Emily", 32, 12000.0)
);

1. Filter

Pre-Java 8 Approach:

List<Employee> filteredEmployees = new ArrayList<>();
for (Employee employee : employees) {
    if (employee.getAge() > 30) {
        filteredEmployees.add(employee);
    }
}

Java 8 Stream Approach:

List<Employee> filteredEmployees = employees.stream()
    .filter(employee -> employee.getAge() > 30)
    .collect(Collectors.toList());

2. Map

Pre-Java 8 Approach:

List<String> employeeNames = new ArrayList<>();
for (Employee employee : employees) {
    employeeNames.add(employee.getName());
}

Java 8 Stream Approach:

List<String> employeeNames = employees.stream()
    .map(Employee::getName)
    .collect(Collectors.toList());

3. Sorting

Pre-Java 8 Approach:

Collections.sort(employees, new Comparator<Employee>() {
    @Override
    public int compare(Employee e1, Employee e2) {
        return e1.getAge().compareTo(e2.getAge());
    }
});

Java 8 Stream Approach:

List<Employee> sortedEmployees = employees.stream()
    .sorted(Comparator.comparing(Employee::getAge))
    .collect(Collectors.toList());

4. Max

Pre-Java 8 Approach:

double maxSalary = Double.MIN_VALUE;
for (Employee employee : employees) {
    if (employee.getSalary() > maxSalary) {
        maxSalary = employee.getSalary();
    }
}

Java 8 Stream Approach:

OptionalDouble maxSalary = employees.stream()
    .mapToDouble(Employee::getSalary)
    .max();

5. Min

Pre-Java 8 Approach:

double minSalary = Double.MAX_VALUE;
for (Employee employee : employees) {
    if (employee.getSalary() < minSalary) {
        minSalary = employee.getSalary();
    }
}

Java 8 Stream Approach:

OptionalDouble minSalary = employees.stream()
    .mapToDouble(Employee::getSalary)
    .min();

6. Average

Pre-Java 8 Approach:

double totalSalary = 0.0;
for (Employee employee : employees) {
    totalSalary += employee.getSalary();
}
double averageSalary = totalSalary / employees.size();

Java 8 Stream Approach:

OptionalDouble averageSalary = employees.stream()
    .mapToDouble(Employee::getSalary)
    .average();

7. Count

Pre-Java 8 Approach:

int count = employees.size();

Java 8 Stream Approach:

long count = employees.stream().count();

8. Grouping

Pre-Java 8 Approach:

Map<Integer, List<Employee>> groupedByAge = new HashMap<>();
for (Employee employee : employees) {
    if (!groupedByAge.containsKey(employee.getAge())) {
        groupedByAge.put(employee.getAge(), new ArrayList<>());
    }
    groupedByAge.get(employee.getAge()).add(employee);
}

Java 8 Stream Approach:

Map<Integer, List<Employee>> groupedByAge = employees.stream()
    .collect(Collectors.groupingBy(Employee::getAge));

As you can see, Java 8 Stream API provides a more declarative way to perform collection operations, which makes the code more readable and concise.

Post a Comment

Previous Post Next Post