首页 > 编程语言 > 详细

Java8排序

时间:2019-03-22 11:53:38      阅读:180      评论:0      收藏:0      [点我收藏+]
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Apple {
    private int wight;
}

排序

List<Integer> numList = new ArrayList<>(Arrays.asList(10, 50, 5, 200, 9, 25));
List<Apple> applelist = new ArrayList<Apple>() {
    {
        add(new Apple(10));
        add(new Apple(50));
        add(new Apple(5));
        add(new Apple(400));
        add(new Apple(9));
    }
};
// 常规升序
numList = numList.stream().sorted().collect(Collectors.toList());
// 常规逆序
numList = numList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
// 根据重量升序
List<Apple> applelist1 = applelist.stream().sorted(Comparator.comparing(Apple::getWight))
        .collect(Collectors.toList());
// 根据重量逆序
List<Apple> applelist2 = applelist.stream().sorted(Comparator.comparing(Apple::getWight).reversed())
        .collect(Collectors.toList());
// lumbda排序
Collections.sort(applelist, (e1, e2) -> {
    return e1.getWight() - e2.getWight();
});

 

Java8排序

原文:https://www.cnblogs.com/zincredible/p/10577255.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!