编程语言
首页 > 编程语言> > Java8排序

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

 

标签:Apple,stream,add,getWight,new,排序,Java8,numList
来源: https://www.cnblogs.com/zincredible/p/10577255.html