
In this article, we will take a look at various examples to Sort a List using Java 8 Stream,
Example 1: using sorted() method - natural sorting order
sorted() method will return a stream that consists of the elements of this stream in a natural sorted order.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Code2care Java Programs
*/
public class Java8ListSortUsingStream {
public static void main(String[] args) {
List<Integer> unsortedList = new ArrayList<>();
unsortedList.add(10);
unsortedList.add(2);
unsortedList.add(5);
unsortedList.add(1);
unsortedList.add(3);
List<Integer> sortedList = unsortedList
.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedList);
}
}
Output: [1, 2, 3, 5, 10]
You can also make use of sorted(Comparator.naturalOrder())
Example 2: Sort in reverse order using Comparator.reverseOrder()
List<Integer> sortedList = unsortedList
.stream()
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
Output: [10, 5, 3, 2, 1]
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Ways to Convert Integer or int to Long in Java - Java
- StringJoiner Example with Java Collections - Java
- How to install Python 2.7.xx on macOS 12.3 Monterey or higher - Python
- Drop table using Java JDBC Template - Java
- Iterate over an Array using Java 8 Stream and foreach - Java
- Fahrenheit to Celsius Temperature Convertor: Tool & Formula - Tools
- How to update all installed packages at once in Ubuntu - Ubuntu
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2) - Android-Studio