Question: How can we sort a list of employees by their salary in reverse order using Java 8 streams?
Answer:
Create an Employee Class:
- First, create an
Employee
class that includes properties like name and salary. This class will represent the structure of your employee objects.
javapublic class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } // Getters and setters for name and salary (not shown for brevity) }- First, create an
Create a List of Employees:
- Next, create a list of
Employee
objects to work with. Populate this list with employee data.
javaList<Employee> employees = new ArrayList<>(); employees.add(new Employee("Alice", 60000)); employees.add(new Employee("Bob", 75000)); employees.add(new Employee("Charlie", 50000)); employees.add(new Employee("David", 90000));- Next, create a list of
Sort Employees by Salary in Reverse Order:
- To sort employees by salary in reverse order, you can use Java 8 streams. Here's the code for this step:
javaList<Employee> sortedEmployees = employees.stream() .sorted(Comparator.comparing(Employee::getSalary).reversed()) .collect(Collectors.toList());
- In this code, we use the
stream()
method to convert the list of employees into a stream. - The
sorted
method is used to perform the sorting. - We pass a comparator that compares employees based on their salary in reverse order using
Comparator.comparing(Employee::getSalary).reversed()
. - Finally, we collect the sorted employees back into a list using
collect(Collectors.toList())
. - Print the Sorted List:
- After sorting, you can print the sorted list to see the results.
javasortedEmployees.forEach(employee -> System.out.println("Name: " + employee.getName() + ", Salary: " + employee.getSalary()));
Example Code:
javaimport java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeSortingExample {
public static void main(String[] args) {
// Create a list of employees
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("Alice", 60000));
employees.add(new Employee("Bob", 75000));
employees.add(new Employee("Charlie", 50000));
employees.add(new Employee("David", 90000));
// Sort employees by salary in reverse order
List<Employee> sortedEmployees = employees.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed())
.collect(Collectors.toList());
// Print the sorted list
sortedEmployees.forEach(employee -> System.out.println("Name: " + employee.getName() + ", Salary: " + employee.getSalary()));
}
}
Sample Output:
outputName: David, Salary: 90000.0
Name: Bob, Salary: 75000.0
Name: Alice, Salary: 60000.0
Name: Charlie, Salary: 50000.0
This code example demonstrates how to sort a list of employees by their salaries in reverse order using Java 8 streams, providing you with a sorted list of employees based on their salary, from highest to lowest.
Comments
Post a Comment