Java Stream sorted()

Learn to use Stream sorted() method to sort the elements in a Stream by their natural order. We can also apply custom orders on the elements using the provided Comparator.

1. Stream sort() Method

The Stream interface provides two methods for sorting the Stream elements.

  • sorted() – Provides the default sorting
  • sorted(Comparator) – Sorting based on provided comparator.

1.1. Stream sorted()

Stream<T> sorted()
  • sorted() is a stateful intermediate operation which returns a new Stream.
  • It seturns a stream consisting of the elements of this stream, sorted according to natural order.
  • If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.
  • For ordered streams, the sort is stable.
  • For unordered streams, no stability guarantees are made.

1.2. Stream sorted(comparator)

Stream<T> sorted(Comparator<? super T> comparator)
  • This is a stateful intermediate operation which returns a new stream.
  • It returns a stream consisting of the elements of this stream, sorted according to the provided Comparator..
  • For ordered streams, the sort is stable.
  • For unordered streams, no stability guarantees are made.

2. Stream sorted() Examples

Example 1: Sorting in Natural Order

In the given Java example, we are sorting a stream of integers in the natural order and printing them into the standard output.

List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);

List<Integer> sortedList = list.stream()
      .sorted()
      .collect(Collectors.toList());

System.out.println(sortedList);

Program output.

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 2: Reverse Ordering

In the given Java example, we are sorting a stream of integers in reverse order using a Comparator and printing them into the standard output.

List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);

List<Integer> sortedList = list.stream()
        .sorted(Comparator.reverseOrder())
        .collect(Collectors.toList());

System.out.println(sortedList);

Program output.

[9, 8, 7, 6, 5, 4, 3, 2, 1]

Example 3: Custom Ordering using Comparator

In the given Java example, we are sorting a stream of integers in reverse order using a custom Comparator.

List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);
 
Comparator<Integer> reverseComparator = new Comparator<Integer>() {
  @Override
  public int compare(Integer i1, Integer i2) {
    return i2.compareTo(i1);
  }
}; 

List<Integer> sortedList = list.stream()
      .sorted(reverseComparator)
      .collect(Collectors.toList());

System.out.println(sortedList);

Program output.

[9, 8, 7, 6, 5, 4, 3, 2, 1]

Example 4: Sorting using Lambda Expressions

Java example to sort a stream of integers in reverse order using lambda expression to specify the comparison logic.

We are rewriting the previous Comparator logic with an inline lambda expression.

List<Integer> list = Arrays.asList(2, 4, 1, 3, 7, 5, 9, 6, 8);

List<Integer> sortedList = list.stream()
      .sorted( (i1, i2) -> i2.compareTo(i1) )
      .collect(Collectors.toList());

System.out.println(sortedList);

Program output.

[9, 8, 7, 6, 5, 4, 3, 2, 1]

Drop me your questions related to Stream sorted() example in Java Stream API.

Happy Learning !!

Sourcecode on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

1 thought on “Java Stream sorted()”

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.