Stream sorted() in Java

Stream sorted() returns a stream consisting of the elements of this stream, sorted according to natural order. For ordered streams, the sort method is stable but for unordered streams, no stability is guaranteed. It is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

Syntax :

Stream<T> sorted()

Where, Stream is an interface and T
is the type of stream elements.

Exception : If the elements of this stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

Below given are some examples to understand the implementation of the function in a better way.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.sorted()
// to get a stream of elements
// sorted in their natural order
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of integers
        List<Integer> list = Arrays.asList(-9, -18, 0, 25, 4);
  
        System.out.println("The sorted stream is : ");
  
        // displaying the stream with elements
        // sorted in natural order
        list.stream().sorted().forEach(System.out::println);
    }
}

chevron_right


Output:

The sorted stream is : 
-18
-9
0
4
25

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Implementation of Stream.sorted()
// to get a stream of elements
// sorted in their natural order
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of strings
        List<String> list = Arrays.asList("Geeks", "for",
                     "GeeksQuiz", "GeeksforGeeks", "GFG");
  
        System.out.println("The sorted stream is : ");
  
        // displaying the stream with elements
        // sorted in their natural order
        list.stream().sorted().forEach(System.out::println);
    }
}

chevron_right


Output:

The sorted stream is : 
GFG
Geeks
GeeksQuiz
GeeksforGeeks
for

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Using stream sorted to sort a stream
// of user defined class objects
import java.util.*;
  
class Point
{
    Integer x, y;
    Point(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }
      
    public String toString() { 
        return this.x + ", "+ this.y;
    
}
  
class GFG
{
    public static void main(String[] args)
    {
  
        List<Point> aList = new ArrayList<>();
        aList.add(new Point(10, 20));
        aList.add(new Point(5, 10));
        aList.add(new Point(1, 100));
        aList.add(new Point(50, 2000));
  
        // displaying the stream with elements
        // sorted according to x coordinate
        aList.stream()
        .sorted((p1, p2)->p1.x.compareTo(p2.x))
        .forEach(System.out::println);
    }
}

chevron_right


Output:

1, 100
5, 10
10, 20
50, 2000


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.