Stream—sort排序(二)

2022-06-28 00:00:00 stream 排序 sort

 

List<Student> list = new ArrayList<>();

Student s1 = new Student("zhangsan","beijing",30,90.0);
list.add(s1);

Student s2 = new Student("lisi","shanghai",29,80.8);
list.add(s2);

Student s3 = new Student("lining","shandong",31,70.8);
list.add(s3);

Student s4 = new Student("fanbingbing","beijing",34,70.0);
list.add(s4);

Student s5 = new Student("libingbing","shanghai",30,95.0);
list.add(s5);

Student s6 = new Student("wangwu","shanghai",30,85.0);
list.add(s6);

// forEach循环
list.stream().forEach(student -> System.out.println(student));

结果:

Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='lisi', address='shanghai', age=29, score=80.8}
Student{name='lining', address='shandong', age=31, score=70.8}
Student{name='fanbingbing', address='beijing', age=34, score=70.0}
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='wangwu', address='shanghai', age=30, score=85.0}

使用stream.sort排序

System.out.println("----------使用stream和sort根据年龄排序--默认升序----------");
// sort排序:原集合不变,新集合按顺序排序
List<Student> sortList1 = list.stream()
    .sorted((a, b) -> a.getAge().compareTo(b.getAge()))
    .collect(Collectors.toList());
sortList1.stream().forEach(s-> System.out.println(s));
System.out.println();

结果:

----------使用stream和sort根据年龄排序--默认升序----------
Student{name='lisi', address='shanghai', age=29, score=80.8}
Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='wangwu', address='shanghai', age=30, score=85.0}
Student{name='lining', address='shandong', age=31, score=70.8}
Student{name='fanbingbing', address='beijing', age=34, score=70.0}

使用stream的sorted排序,根据年龄倒序排序

System.out.println("---------使用stream和sort根据年龄排序--降序排列-----------");
List<Student> sortDesList = list.stream()
    .sorted(Comparator.comparingInt(Student::getAge).reversed())
    .collect(Collectors.toList());
sortDesList.stream().forEach(s-> System.out.println(s));
System.out.println();

结果:

---------使用stream和sort根据年龄排序--降序排列-----------
Student{name='fanbingbing', address='beijing', age=34, score=70.0}
Student{name='lining', address='shandong', age=31, score=70.8}
Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='wangwu', address='shanghai', age=30, score=85.0}
Student{name='lisi', address='shanghai', age=29, score=80.8}

使用stream的sorted根据多字段排序,根据年龄降序然后根据分数升序排序。

System.out.println("---------使用steam和sort根据多字段排序");
// 首先根据年龄降序排列,在根据成绩升序排序
Stream<Student> sortMoreList = list.stream().sorted(
    Comparator.comparingInt(Student::getAge).reversed()
    .thenComparing(Student::getScore));
sortMoreList.forEach(s-> System.out.println(s));
System.out.println();

结果:

---------使用steam和sort根据多字段排序
Student{name='fanbingbing', address='beijing', age=34, score=70.0}
Student{name='lining', address='shandong', age=31, score=70.8}
Student{name='wangwu', address='shanghai', age=30, score=85.0}
Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='lisi', address='shanghai', age=29, score=80.8}

分组排序

System.out.println("--------使用stream和sort分组排序");
// 根据地点进行分组,组内按照分数降序排序;注意,如果此时有多个字段排序是有问题的,也就是说分组后的排序是不准确的。只有一个排序时是没问题的。
Map<String, List<Student>> sortAndGroupMap = list.stream().sorted(
                    Comparator.comparingDouble(Student::getScore).reversed()
                ).collect(Collectors.groupingBy(Student::getAddress));

sortAndGroupMap.get("shandong").stream().forEach(s-> System.out.println(s));
System.out.println("------------");
sortAndGroupMap.get("beijing").stream().forEach(s-> System.out.println(s));
System.out.println("------------");
sortAndGroupMap.get("shanghai").stream().forEach(s-> System.out.println(s));

结果:

--------使用stream和sort分组排序
Student{name='lining', address='shandong', age=31, score=70.8}
------------
Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='fanbingbing', address='beijing', age=34, score=70.0}
------------
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='wangwu', address='shanghai', age=30, score=85.0}
Student{name='lisi', address='shanghai', age=29, score=80.8}

使用集合自身sort排序

System.out.println("----------不使用stream和sort------------");
// 使用集合的sort排序,集合自身排序发生变化
list.sort((a,b)->a.getAge().compareTo(b.getAge()));
list.stream().forEach(student -> System.out.println(student));
System.out.println();

结果:

---------不适用stream和sort------------
Student{name='lisi', address='shanghai', age=29, score=80.8}
Student{name='zhangsan', address='beijing', age=30, score=90.0}
Student{name='libingbing', address='shanghai', age=30, score=95.0}
Student{name='wangwu', address='shanghai', age=30, score=85.0}
Student{name='lining', address='shandong', age=31, score=70.8}
Student{name='fanbingbing', address='beijing', age=34, score=70.0}

利用stream替换集合中对象的某一属性值

// list集合某一属性全部替换固定值
list.stream().forEach(student -> student.setAddress("china"));
System.out.println();

 

    原文作者:cdw_sunshine
    原文地址: https://blog.csdn.net/cdw_sunshine/article/details/90739529
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章