Comparator
interface and the Stream
API introduced in Java 8, including both ascending and descending orders.Table of Contents
- Introduction
- Employee Class Definition
- Sorting by Name (Ascending and Descending)
- Sorting by Salary (Ascending and Descending)
- Sorting by Name and Salary (Ascending and Descending)
- Conclusion
Introduction
In Java, sorting a collection of objects can be done using the Comparator
interface and the Stream
API. This guide will demonstrate how to sort a list of employees by name and salary, both in ascending and descending order.
Employee Class Definition
First, let's define the Employee
class with name
and salary
attributes.
Example
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Employee{name='" + name + "', salary=" + salary + '}';
}
}
Sorting by Name (Ascending and Descending)
To sort a list of employees by name, you can use the Comparator
interface with the Stream
API.
Example (Ascending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
// Sort by name in ascending order
List<Employee> sortedByNameAsc = employees.stream()
.sorted(Comparator.comparing(Employee::getName))
.collect(Collectors.toList());
System.out.println("Sorted by Name (Ascending):");
sortedByNameAsc.forEach(System.out::println);
}
}
Example (Descending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
// Sort by name in descending order
List<Employee> sortedByNameDesc = employees.stream()
.sorted(Comparator.comparing(Employee::getName).reversed())
.collect(Collectors.toList());
System.out.println("Sorted by Name (Descending):");
sortedByNameDesc.forEach(System.out::println);
}
}
Explanation
- The
Comparator.comparing
method creates a comparator that compares employees by their names. - The
reversed
method is used to reverse the order for descending sorting. - The
sorted
method sorts the stream of employees using the comparator. - The
collect
method collects the sorted stream into a list.
Output:
Sorted by Name (Ascending):
Employee{name='Jack', salary=40000.0}
Employee{name='Jane', salary=60000.0}
Employee{name='John', salary=50000.0}
Sorted by Name (Descending):
Employee{name='John', salary=50000.0}
Employee{name='Jane', salary=60000.0}
Employee{name='Jack', salary=40000.0}
Sorting by Salary (Ascending and Descending)
To sort a list of employees by salary, you can use the Comparator
interface with the Stream
API.
Example (Ascending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
// Sort by salary in ascending order
List<Employee> sortedBySalaryAsc = employees.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary))
.collect(Collectors.toList());
System.out.println("Sorted by Salary (Ascending):");
sortedBySalaryAsc.forEach(System.out::println);
}
}
Example (Descending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
// Sort by salary in descending order
List<Employee> sortedBySalaryDesc = employees.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
.collect(Collectors.toList());
System.out.println("Sorted by Salary (Descending):");
sortedBySalaryDesc.forEach(System.out::println);
}
}
Explanation
- The
Comparator.comparingDouble
method creates a comparator that compares employees by their salaries. - The
reversed
method is used to reverse the order for descending sorting. - The
sorted
method sorts the stream of employees using the comparator. - The
collect
method collects the sorted stream into a list.
Output:
Sorted by Salary (Ascending):
Employee{name='Jack', salary=40000.0}
Employee{name='John', salary=50000.0}
Employee{name='Jane', salary=60000.0}
Sorted by Salary (Descending):
Employee{name='Jane', salary=60000.0}
Employee{name='John', salary=50000.0}
Employee{name='Jack', salary=40000.0}
Sorting by Name and Salary (Ascending and Descending)
To sort a list of employees first by name and then by salary, you can combine comparators using the thenComparing
method.
Example (Ascending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
employees.add(new Employee("John", 45000));
// Sort by name and then by salary in ascending order
List<Employee> sortedByNameAndSalaryAsc = employees.stream()
.sorted(Comparator.comparing(Employee::getName)
.thenComparingDouble(Employee::getSalary))
.collect(Collectors.toList());
System.out.println("Sorted by Name and Salary (Ascending):");
sortedByNameAndSalaryAsc.forEach(System.out::println);
}
}
Example (Descending)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortEmployeeExample {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
employees.add(new Employee("John", 50000));
employees.add(new Employee("Jane", 60000));
employees.add(new Employee("Jack", 40000));
employees.add(new Employee("John", 45000));
// Sort by name and then by salary in descending order
List<Employee> sortedByNameAndSalaryDesc = employees.stream()
.sorted(Comparator.comparing(Employee::getName).reversed()
.thenComparing(Comparator.comparingDouble(Employee::getSalary).reversed()))
.collect(Collectors.toList());
System.out.println("Sorted by Name and Salary (Descending):");
sortedByNameAndSalaryDesc.forEach(System.out::println);
}
}
Explanation
- The
Comparator.comparing
method creates a comparator that compares employees by their names. - The
thenComparingDouble
method adds a secondary comparator that compares employees by their salaries. - The
reversed
method is used to reverse the order for descending sorting. - The
sorted
method sorts the stream of employees using the combined comparator. - The
collect
method collects the sorted stream into a list.
Output:
Sorted by Name and Salary (Ascending):
Employee{name='Jack', salary=40000.0}
Employee{name='Jane', salary=60000.0}
Employee{name='John', salary=45000.0}
Employee{name='John', salary=50000.0}
Sorted by Name and Salary (Descending):
Employee{name='John', salary=50000.0}
Employee{name='John', salary=45000.0}
Employee{name='Jane', salary=60000.0}
Employee{name='Jack', salary=40000.0}
Conclusion
Sorting a list of employees by name and salary in Java 8 can be accomplished using the Comparator
interface and the Stream
API, both in ascending and descending order. The Comparator.comparing
method provides a straightforward way to create a comparator for sorting by a single attribute, while the thenComparing
method allows for sorting by multiple attributes. The reversed
method can be used to sort in descending order. Depending on your specific use case and requirements, you can choose the method that best fits your needs.
Comments
Post a Comment
Leave Comment