使用sorted排序

  1. public static void main(String[] args) {
  2. List<Student> list = new ArrayList<>();
  3. Student s1 = new Student("zhangsan","beijing",30);
  4. list.add(s1);
  5. Student s2 = new Student("lisi","shanghai",29);
  6. list.add(s2);
  7. Student s3 = new Student("lining","shandong",31);
  8. list.add(s3);
  9. // forEach循环
  10. list.stream().forEach(student -> System.out.println(student.getAge()));
  11. System.out.println("----------使用stream和sort--默认升序----------");
  12. // sort排序:原集合不变,新集合按顺序排序
  13. List<Student> sortList1 = list.stream().sorted((a, b) -> a.getAge().compareTo(b.getAge())).collect(Collectors.toList());
  14. sortList1.stream().forEach(s-> System.out.println(s.getAge()));
  15. System.out.println();
  16. System.out.println("---------使用stream和sort--降序排列-----------");
  17. List<Student> sortDesList = list.stream().sorted(Comparator.comparingInt(Student::getAge).reversed()).collect(Collectors.toList());
  18. sortDesList.stream().forEach(s-> System.out.println(s.getAge()));
  19. System.out.println();
  20. System.out.println("----------不使用stream和sort------------");
  21. // 使用集合的sort排序,集合自身排序发生变化
  22. list.sort((a,b)->a.getAge().compareTo(b.getAge()));
  23. list.stream().forEach(student -> System.out.println(student.getAge()));
  24. System.out.println();
  25. }

运行结果

  1. 运行结果:
  2. Connected to the target VM, address: '127.0.0.1:51549', transport: 'socket'
  3. 30
  4. 29
  5. 31
  6. ----------使用streamsort--默认升序----------
  7. 29
  8. 30
  9. 31
  10. ---------使用streamsort--降序排列-----------
  11. Disconnected from the target VM, address: '127.0.0.1:51549', transport: 'socket'
  12. 31
  13. 30
  14. 29
  15. ----------不使用streamsort------------
  16. 29
  17. 30
  18. 31

stream().forEach()

  1. List<Student> list = new ArrayList<>();
  2. Student s1 = new Student("zhangsan","beijing",30,90.0);
  3. list.add(s1);
  4. Student s2 = new Student("lisi","shanghai",29,80.8);
  5. list.add(s2);
  6. Student s3 = new Student("lining","shandong",31,70.8);
  7. list.add(s3);
  8. Student s4 = new Student("fanbingbing","beijing",34,70.0);
  9. list.add(s4);
  10. Student s5 = new Student("libingbing","shanghai",30,95.0);
  11. list.add(s5);
  12. Student s6 = new Student("wangwu","shanghai",30,85.0);
  13. list.add(s6);
  14. // forEach循环
  15. list.stream().forEach(student -> System.out.println(student));

结果

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

使用stream.sort排序

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

结果

  1. ----------使用streamsort根据年龄排序--默认升序----------
  2. Student{name='lisi', address='shanghai', age=29, score=80.8}
  3. Student{name='zhangsan', address='beijing', age=30, score=90.0}
  4. Student{name='libingbing', address='shanghai', age=30, score=95.0}
  5. Student{name='wangwu', address='shanghai', age=30, score=85.0}
  6. Student{name='lining', address='shandong', age=31, score=70.8}
  7. Student{name='fanbingbing', address='beijing', age=34, score=70.0}

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

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

结果

  1. ---------使用streamsort根据年龄排序--降序排列-----------
  2. Student{name='fanbingbing', address='beijing', age=34, score=70.0}
  3. Student{name='lining', address='shandong', age=31, score=70.8}
  4. Student{name='zhangsan', address='beijing', age=30, score=90.0}
  5. Student{name='libingbing', address='shanghai', age=30, score=95.0}
  6. Student{name='wangwu', address='shanghai', age=30, score=85.0}
  7. Student{name='lisi', address='shanghai', age=29, score=80.8}

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

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

结果

  1. ---------使用steamsort根据多字段排序
  2. Student{name='fanbingbing', address='beijing', age=34, score=70.0}
  3. Student{name='lining', address='shandong', age=31, score=70.8}
  4. Student{name='wangwu', address='shanghai', age=30, score=85.0}
  5. Student{name='zhangsan', address='beijing', age=30, score=90.0}
  6. Student{name='libingbing', address='shanghai', age=30, score=95.0}
  7. Student{name='lisi', address='shanghai', age=29, score=80.8}

分组排序

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

结果

  1. --------使用streamsort分组排序
  2. Student{name='lining', address='shandong', age=31, score=70.8}
  3. ------------
  4. Student{name='zhangsan', address='beijing', age=30, score=90.0}
  5. Student{name='fanbingbing', address='beijing', age=34, score=70.0}
  6. ------------
  7. Student{name='libingbing', address='shanghai', age=30, score=95.0}
  8. Student{name='wangwu', address='shanghai', age=30, score=85.0}
  9. Student{name='lisi', address='shanghai', age=29, score=80.8}

使用集合自身sort排序

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

结果

  1. ---------不适用streamsort------------
  2. Student{name='lisi', address='shanghai', age=29, score=80.8}
  3. Student{name='zhangsan', address='beijing', age=30, score=90.0}
  4. Student{name='libingbing', address='shanghai', age=30, score=95.0}
  5. Student{name='wangwu', address='shanghai', age=30, score=85.0}
  6. Student{name='lining', address='shandong', age=31, score=70.8}
  7. Student{name='fanbingbing', address='beijing', age=34, score=70.0}

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

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

转载 https://blog.csdn.net/cdw_sunshine/article/details/90739529?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.essearch_pc_relevant