- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Can we sort a list with Lambda in Java?
Yes, we can sort a list with Lambda. Let us first create a String List:
List<String> list = Arrays.asList("LCD","Laptop", "Mobile", "Device", "LED", "Tablet");
Now, sort using Lambda, wherein we will be using compareTo():
Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1));
The following is an example to sort a list with Lambda in Java:
Example
import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String... args) { List<String> list = Arrays.asList("LCD","Laptop", "Mobile", "Device", "LED", "Tablet"); System.out.println("List = "+list); Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1)); System.out.println("Sorted List = "+list); } }
Output
List = [LCD, Laptop, Mobile, Device, LED, Tablet] Sorted List = [Tablet, Mobile, Laptop, LED, LCD, Device]
- Related Articles
- How can we sort a Map by both key and value using lambda in Java?
- How can we use lambda expressions with functional interfaces in Java?
- How can we write a multiline lambda expression in Java?
- How can we sort a JSONArray in Java?
- Can we use Comparator with list in Java?
- How can we pass lambda expression in a method in Java?
- How can we iterate the elements of List and Map using lambda expression in Java?
- How can we write Callable as a lambda expression in Java?
- How can we sort a JSONObject in Java?\n
- What kind of variables can we access in a lambda expression in Java?
- Java Program to create a new list with values from existing list with Lambda Expressions
- How can we sort the items of a JComboBox in Java?
- Can we insert null values in a Java list?
- How can we sort a JTable on a particular column in Java?
- Can we convert a list to a Set in Java?

Advertisements