List两种排序方法,sort,stream

       Subject subject1 = new Subject("语文", 91.0);
        Subject subject2 = new Subject("语文", 94.0);
        Subject subject3 = new Subject("语文", 92.0);
        Subject subject4 = new Subject("语文", 90.0);
        List<Subject> subjectList = new ArrayList<>();
        subjectList.add(subject1);
        subjectList.add(subject2);
        subjectList.add(subject3);
        subjectList.add(subject4);
        List<Subject> subjectListNew = new ArrayList<>();

 //排序生效,使用stream和collect
        subjectListNew = subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade())).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(subjectListNew));
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]
        System.out.println(JSON.toJSONString(subjectList));//原集合没变
        //[{"grade":91,"id":1,"name":"语文"},{"grade":94,"id":2,"name":"数学"},{"grade":92,"id":3,"name":"英语"},{"grade":90,"id":4,"name":"语文"}]

        //排序生效,不使用stream和collect
        subjectList.sort((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
        System.out.println(JSON.toJSONString(subjectList));//原集合变化
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]

        //排序不生效,使用stream时需要在末尾collect
        subjectList.stream().sorted((s1, s2) -> s1.getGrade().compareTo(s2.getGrade()));
        System.out.println(JSON.toJSONString(subjectList));
        //[{"grade":91.0,"name":"语文"},{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":90.0,"name":"语文"}]

        //分数升序排列
        subjectListNew = subjectList.stream().sorted((s1, s2) -> {
            return s1.getGrade().compareTo(s2.getGrade());
        }).collect(Collectors.toList());//如果使用分号,需要加{}和return
        System.out.println(JSON.toJSONString(subjectListNew));
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]

        //分数降序排列
        subjectListNew = subjectList.stream().sorted(Comparator.comparingDouble(Subject::getGrade).reversed()).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(subjectListNew));
        //[{"grade":94.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":90.0,"name":"语文"}]

        //分数升序排列
        subjectListNew = subjectList.stream().sorted(Comparator.comparingDouble(Subject::getGrade)).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(subjectListNew));
        //[{"grade":90.0,"name":"语文"},{"grade":91.0,"name":"语文"},{"grade":92.0,"name":"语文"},{"grade":94.0,"name":"语文"}]
public class Subject {

    String name;
    Double grade;

    public Subject(String name, Double grade) {
        this.name = name;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getGrade() {
        return grade;
    }

    public void setGrade(Double grade) {
        this.grade = grade;
    }
}

以上基于jdk1.8

.stream().sorted.....collect():返回新的有序集合,原集合顺序不变

.sort...:返回void,原集合顺序变为有序

欢迎关注我的公众号

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页