Java8-排序

@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();
});

 

posted @ 2019-03-22 11:39  zincredible  阅读(1144)  评论(0编辑  收藏  举报