- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to sort String Stream with reversed Comparator
Let us first create a String List:
List<String> list = Arrays.asList("Tom", "Jack", "Ryan", "Kevin", "Loki", "Thor");
Now compare each element for reverse:
Comparator<String> comp = (aName, bName) -> aName.compareTo(bName);
Now, perform sort:
list.stream().sorted(comp.reversed())
The following is an example to sort String Stream with reversed Comparator:
Example
import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo { public static void main(String[] args) { List<String> list = Arrays.asList("Tom", "Jack", "Ryan", "Kevin", "Loki", "Thor"); System.out.println("Initial List = "+list); System.out.println("Reverse..."); Comparator<String> comp = (aName, bName) -> aName.compareTo(bName); list.stream().sorted(comp.reversed()) .forEach(System.out::println); } }
Output
Initial List = [Tom, Jack, Ryan, Kevin, Loki, Thor] Reverse... Tom Thor Ryan Loki Kevin Jack
- Related Articles
- How to sort an array with customized Comparator in Java?
- Java Program to sort Integer list in reversed order
- Java Program to get maximum value with Comparator
- Java Program to get minimum value with Comparator
- Sort ArrayList in Descending order using Comparator with Java Collections
- Java Program to create a TreeSet with custom Comparator
- Java Program to Sort a String
- How to sort a list using Comparator with method reference in Java 8?\n
- Java Program to create Stream from a String/Byte Array
- How to sort List in descending order using Comparator in Java
- How to sort a collection by using Stream API with lambdas in Java?
- How to filter String stream and map to lower case in Java? Perform sort as well.
- C Program for Reversed String Pattern
- How to sort a list by name using a Comparator interface in Java?
- Java Program to Map String list to lowercase and sort

Advertisements