我们可以使用Java中的Lambda对列表进行排序吗?
是的,我们可以使用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]
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短