
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,
- How to get Java Thread name Example [Program]
- Java 8: Get First and Last Date of the Week for Given Date
- Convert String to int in Java
- How to Get List of All Country Codes in Java Using Locale Class
- Convert Multidimensional Array toString In Java
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Create a Zip file using Java Code programmatically
- [Fix] java.net.MalformedURLException: unknown protocol
- [fix] Java JDBC ConnectException: Connection refused
- Read Json File and Convert to Java Object using Jackson
- list of jars required for hibernate 4.x.x
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java: The value of the local variable string is not used
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Run SQL Script file using Java JDBC Code Example
- Remove Trailing zeros BigDecimal Java
- Java JDBC IN Clause Example with PreparedStatement MySQL
- Convert Java List to Json String using Jackson
- Java 8 foreach loop code examples
- error: file not found: HelloWorld.java
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Struts 2 Hello World Example in Eclipse
More Posts:
- error CAML Query containing special characters - SharePoint
- Error when deleting SharePoint Online folder or file - SharePoint
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error - Android
- Uber and UberEats App down - issues with network, login, navigation, fare price, orders - News
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows - Git
- SharePoint Server 2016 installation System Hardware requirements - SharePoint
- How to Stop Photos App from auto loading when device connected to the Mac - Mac-OS-X
- iOS Fix: This app is no longer shared with you. To use it, you must buy it from the App Store. - Apple