Java Stream sorted example

Previous
Next

In this post, we will see how to sort a list with Stream.sorted() method.

Java 8 has introduced Stream.sort() method to sort list of elements conveniently. It helps us to write short and concise functional style code rather than boilerplate code.


java.util.Stream has two overloaded versions of sorted() method.

  1. sorted(): Returns a stream having elements sorted by natural order
  2. sorted(Comparator<? super T> comparator): Returns a stream having elements sorted by provided comparator

💡 Did you know?

If you sort list of Integers using Stream.sorted() method then Comparable interface, implemented by Integer class, will define natural ordering of list of Integers .

Let’s understand how can we use Stream.sorted() to sort list of elements.

Sort List of Integers

We can simply use sorted() method to sort list of integers.

Here, List of Integers is sorted by Comparable interface implemented by Integer class.

As you can see, Integers are compared on this basis of (x < y) ? -1 : ((x == y) ? 0 : 1) logic.

You can pass Comparator.reverseOrder() to sorted method to reverse sort list of Integers.

Comparator.reverseOrder() is static method and provides a comparator that imposes reverse of natural ordering.

Let’s see complete example to sort list of integers.

Output:

[20, 21, 34, 37, 40]
[40, 37, 34, 21, 20]

Sort List of Strings

We can simply use sorted() method to sort list of Strings.

Here, List of Strings is sorted by Comparable interface implemented by String class.

As you can see, Integers are compared on this basis of (x < y) ? -1 : ((x == y) ? 0 : 1) logic.

You can pass Comparator.reverseOrder() to sorted method to reverse sort list of Integers.

Comparator.reverseOrder() is static method and provides a comparator that imposes reverse of natural ordering.

Let’s see complete example to sort list of integers.

Output:

[C++, Java, PHP, Python]
[Python, PHP, Java, C++]

Sort List of custom objects

Sort list of Students by natural order

Create a class named Student.java

Let’s use Stream’s sorted() to sort the list of Students now.

Let’s run above program.

Exception in thread “main” java.lang.ClassCastException: org.arpit.java2blog.Student cannot be cast to java.lang.Comparable
at java.util.Comparators$NaturalOrderComparator.compare(Comparators.java:47)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
at java.util.TimSort.sort(TimSort.java:220)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:353)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
at org.arpit.java2blog.SortListOfStudents.main(SortListOfStudents.java:14)

Did you know why we got exception here?
We got the ClassCastException exception because we did not implement Comparable interface in Student class.

Let’s implement Comparable interface in Student class.

Run SortListOfStudents again, and you will get below output.

[Student [name=Andy, age=17], Student [name=Harshal, age=18], Student [name=Mary, age=20], Student [name=Peter, age=21], Student [name=Peter, age=19]]

As you can see, list of Students is sorted by student name.

Sort list of Students by reverse natural order

Sort list of Students by name in descending order.

Output:

[Student [name=Peter, age=21], Student [name=Peter, age=19], Student [name=Mary, age=20], Student [name=Harshal, age=18], Student [name=Andy, age=17]]

Sort list of Students by age using comparator

Output:

[Student [name=Andy, age=17], Student [name=Harshal, age=18], Student [name=Peter, age=19], Student [name=Mary, age=20], Student [name=Peter, age=21]]

You can also use Comparator.comparing(Function<? super T, ? extends U> keyExtractor) to sort it on the basis of age.
Comparator.comparing() accepts a function that maps a sort key from a type, and returns a Comparator that compares by that sort key.
Let’s say you want to sort list of Students on the basis of age. You can extracts sort key age from Student object and Comparator.comparing() will return a comparator which will sort by that sort key.

Here Comparator.comparing() returns a new Comparator to sort based on age.

We can also use method reference here as we are just calling s.getAge() in the functional interface.

Let’s say you want to sort list of Students on the basis of age in descending order. You can pass another comparator to Comparator.comparing() to make custom sorting based on sort key.

Here,
Comparator.comparing() has two arguments.
Student::getAge to define sort key.
(age1,age2) -> age2 - age1) to define custom sorting on the basis of sort key.

Here is the complete example.

Output:

Sorted list by age ascending: [Student [name=Andy, age=17], Student [name=Harshal, age=18], Student [name=Peter, age=19], Student [name=Mary, age=20], Student [name=Peter, age=21]]
Sorted list by age descending: [Student [name=Peter, age=21], Student [name=Mary, age=20], Student [name=Peter, age=19], Student [name=Harshal, age=18], Student [name=Andy, age=17]]

Sort list of Students by name and age

Let’s say you want to list of students by name and if name is same, then you need to sort by age.

You can use Comparator.thenComparing() with Comparator.comparing() to achieve the same.

Let’s see with the help of example.

Output:

[Student [name=Andy, age=17], Student [name=Harshal, age=18], Student [name=Mary, age=20], Student [name=Peter, age=19], Student [name=Peter, age=21]]

As you can see, there are two students named Peter in the list then, sorted by age.

Excercise

Given a list of Employee objects, you need to sort them of Employee’s name in descending order and return a sorted list of Employee(List<Employee>)
Here is the definition of Employee class.

Previous
Next

Add Comment