Java Stream sorted example

Java Stream Sorted

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 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)
Here is the definition of Employee class.

That’s all about Java Stream sorted example.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTimeWas this post helpful? In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffsetWas this post helpful? In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8]Was this post helpful? In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom formatWas this post helpful? In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTimeWas this post helpful? In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-62b6ef2817c70581433926/] Here are […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 ‚Äď Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()ConclusionWas this post helpful? Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.