是的,我们可以使用Lambda对列表进行排序。让我们首先创建一个字符串列表:
List<String> list = Arrays.asList("LCD","Laptop", "Mobile", "Device", "LED", "Tablet");
现在,使用Lambda进行排序,其中我们将使用compareTo()
:
Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1));
以下是在Java中使用Lambda对列表进行排序的示例:
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); } }
输出结果
List = [LCD, Laptop, Mobile, Device, LED, Tablet] Sorted List = [Tablet, Mobile, Laptop, LED, LCD, Device]