- 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
Advertisements