Course Content
Stream API
Stream API
Arranging Items in Order with the sorted() Method
In Java's Stream API, the sorted()
method allows you to sort stream elements efficiently. Sorting is a crucial operation because it simplifies data processing, making it easier to arrange elements in the desired order.
Natural Order Sorting
The sorted()
method is overloaded and has two versions. Let's start with the first one:
This version sorts elements based on their natural ordering. To use this method, the elements must implement the Comparable
interface, which allows them to be automatically compared and ordered.
Practical Example
Imagine a factory with various parts, each having a unique weight. The goal is to sort these parts in ascending order based on their weight using the Comparable
interface.
Main
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { // List of factory parts List<Part> parts = Arrays.asList( new Part("Wheel", 10), new Part("Engine", 250), new Part("Brake", 15), new Part("Chassis", 80) ); // Sorting parts by weight in ascending order parts.stream() .sorted() // Sorting by natural order .forEach(System.out::println); } } class Part implements Comparable<Part> { private String partName; private int weight; public Part(String partName, int weight) { this.partName = partName; this.weight = weight; } public String getPartName() { return partName; } public int getWeight() { return weight; } @Override public int compareTo(Part other) { return Integer.compare(this.weight, other.weight); // Sorting by weight } @Override public String toString() { return partName + ": " + weight + "kg"; } }
Here, the Part
class implements the Comparable<Part>
interface. The compareTo()
method sorts parts by weight
using Integer.compare(this.weight, other.weight)
.
In the main class, the sorted()
method sorts the stream of parts in ascending order based on their weight
.
Sorting with a Comparator
The second version of the sorted()
method allows sorting using a custom Comparator
. This method takes a Comparator
as a parameter, defining how elements should be compared and ordered.
With this approach, you can specify a custom sorting order, giving us more control over how elements are arranged.
Practical Example
Suppose you need to sort a list of factory employees by salary in descending order. You'll use the sorted()
method with a comparator to achieve this.
Main
package com.example; import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { // List of factory employees List<Employee> employees = Arrays.asList( new Employee("John", 50000), new Employee("Emily", 75000), new Employee("Michael", 45000), new Employee("Sophia", 60000) ); // Sorting employees by salary in descending order employees.stream() .sorted(Comparator.comparingInt(Employee::getSalary).reversed()) // Sorting in descending order .forEach(System.out::println); } } class Employee { private String name; private int salary; public Employee(String name, int salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public int getSalary() { return salary; } @Override public String toString() { return name + ": " + salary; } }
Here, you used the comparator Comparator.comparingInt(Employee::getSalary).reversed()
, which first sorts employees by salary in ascending order and then applies reversed()
to switch it to descending order.
Thanks for your feedback!