What is Stream.sorted() method in Java?
Overview
The Stream.sorted() is the method of the Stream interface that returns a sorted stream. The elements of the stream are sorted according to their natural order or according to a provided Comparator. The sorted stream is new and does not affect the ordering of the elements in the original stream. This method is an intermediate operation and can be chained with other Stream operations.
Syntax
The syntax of the sorted() method is as follows:
stream.sorted()
Parameters
This method does not take any parameters.
Return value
This method returns a sorted stream.
Exceptions
This method may throw a ClassCastException, if the elements in the stream are not Comparable.
Code examples
The code example below shows how to use the sorted() method:
The sorted() method
Let's look at the code below:
import java.util.*;import java.util.stream.*;class StreamExample {public static void main(String[] args){Stream<Integer> stream = Stream.of(1, 3, 2, 6, 5, 8);stream.sorted().forEach(System.out::println);}}
Code explanation
- Line 7: We create a stream of integers.
- Line 8: We invoke the
sorted()method, which returns a sorted stream. TheforEach()method prints each element of the sorted stream on a new line. It is an intermediate operation and does not affect the elements in the original stream.
The sorted() method with Comparator
We may use the sorted() method to sort the elements in reverse order by passing a Comparator as an argument. Let's look at the code below:
import java.util.*;import java.util.stream.*;class StreamExample {public static void main(String[] args){Stream<Integer> stream = Stream.of(1, 3, 2, 6, 5, 8);stream.sorted((a, b)->b-a).forEach(System.out::println);}}
Code explanation
- Line 7: We create a stream of integers.
- Line 8: We call the
sorted()method with a customComparatorthat sorts the elements in reverse order.
Sort objects of a user-defined class
We use the sorted() method to sort objects of a user-defined class by passing a Comparator as an argument. Let's look at the code below:
import java.util.*;import java.util.stream.*;class Employee {int id, salary;String name, designation;// constructorEmployee(int id,String name,int salary,String designation){this.id = id;this.name = name;this.salary = salary;this.designation = designation;}public String toString() {return "Employee{id=" + this.id+ ", name=\'" + this.name+ "\', salary=" + this.salary+ ", designation=\'"+ this.designation + "\'}";}}class Example {public static void main(String[] args){List<Employee> list = new ArrayList<Employee>();// adding elements to the listlist.add(new Employee(1,"John",45000,"Manager"));list.add(new Employee(2,"Martin",40000,"Developer"));list.add(new Employee(3,"Sam",35000,"tester"));// sorting the list according to the salary// in ascending orderStream<Employee> stream = list.stream();stream.sorted((e1, e2) -> e1.salary - e2.salary).forEach(System.out::println);}}
Code explanation
- Line 5: We create a
Employeeclass with four fields:id,name,salary, anddesignation. - Lines 30 to 35: We create a list of
Employeeobjects and add three elements to the list. - Line 39: We obtain a stream from the list using the
stream()method. - Line 40: We call the
sorted()method with a customComparatorthat compares twoEmployeeobjects by their salaries. Then, we use theforEach()method to print each element of the sorted stream on a new line.