首页 > 其他分享 >Stream流sorted的多级排序问题(巨坑)

Stream流sorted的多级排序问题(巨坑)

时间:2024-04-07 15:11:38浏览次数:16  
标签:studentArrayList Stream reversed 巨坑 getTestScore Student sorted 倒序 thenComparing

https://blog.csdn.net/qq_28165595/article/details/131152763

 

前言
之前在给一个List集合中对象进行多属性排序,比如先根据对象中的A属性正序排序,若两个对象的A属性值相同,再根据对象的B属性进行正序排序。这种排序在实际开发中比较常见,但是这里面有个坑。

举个例子
先初始化一个学生集合

List<Student> studentArrayList = new ArrayList<>();
studentArrayList.add(new Student("张三", 20, 34));
studentArrayList.add(new Student("李四", 20, 35));
studentArrayList.add(new Student("王五", 22, 34));
studentArrayList.add(new Student("丁飞", 21, 23));
studentArrayList.add(new Student("曹海", 22, 23));
studentArrayList.add(new Student("徐风", 24, 234));


学生类如下

public class Student {
private String name;
private Integer age;
private Integer testScore;

public Student(String name, Integer age, Integer testScore) {
this.name = name;
this.age = age;
this.testScore = testScore;
}

//省略set,get方法
}
如果我们要先根据学生年龄倒序排,再根据学生考试成绩倒序排,很多人可以直接写下这样的排序代码

List studentArraySortList = studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().
thenComparingInt(Student::getTestScore).reversed()).collect(Collectors.toList());
运行结果如下

[姓名=李四,年龄:20分数:35, 姓名=张三,年龄:20分数:34, 姓名=丁飞,年龄:21分数:23,
姓名=王五,年龄:22分数:34, 姓名=曹海,年龄:22分数:23, 姓名=徐风,年龄:24分数:234]
很明显,这不是我们预期的效果

 

 

如何正确实现多级排序
我们先来看看reversed()方法的方法说明

 

 

大致意思事说在当前的比较器基础上运行新的比较器。

 

 

这里的reversed是运用于Comparator.comparingInt(Student::getAge).reversed().
thenComparingInt(Student::getTestScore)而不是thenComparingInt(Student::getTestScore),这样是不是很好理解了。所以我们要想先根据学生年龄倒序排,再根据学生考试成绩倒序排,应该这样写

List<Student> studentArraySortList = studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).
thenComparingInt(Student::getTestScore).reversed()).collect(Collectors.toList());

 


感兴趣的小伙伴可以测试下四种写法,对比下运行结果,加深理解

//先根据age正序排,再根据testScore正序排
studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).
thenComparingInt(Student::getTestScore)).collect(Collectors.toList()).forEach(System.out::println);


//先根据age倒序排,再根据testScore正序排
studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().
thenComparingInt(Student::getTestScore)).collect(Collectors.toList()).forEach(System.out::println);

//先根据age倒序排,再根据testScore倒序排
studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).
thenComparingInt(Student::getTestScore).reversed()).collect(Collectors.toList()).forEach(System.out::println);

//先根据age正序排,再根据testScore倒序排
studentArrayList.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().
thenComparingInt(Student::getTestScore).reversed()).collect(Collectors.toList()).forEach(System.out::println);

 

标签:studentArrayList,Stream,reversed,巨坑,getTestScore,Student,sorted,倒序,thenComparing
From: https://www.cnblogs.com/kelelipeng/p/18119108

相关文章

  • LeetCode 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows
    原题链接在这里:https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/description/题目:Youaregivenan mxn matrix mat thathasitsrowssortedinnon-decreasingorderandaninteger k.Youareallowedtochoose exactlyo......
  • DISTILLM: Towards Streamlined Distillation for Large Language Models
    本文是LLM系列文章,针对《DISTILLM:TowardsStreamlinedDistillationforLargeLanguageModels》的翻译。DISTILLM:面向大型语言模型的流线蒸馏摘要1引言2背景3DISTILLM4实验5分析与讨论6相关工作7结论摘要知识蒸馏(KD)被广泛用于将教师模型压缩为......
  • Ubuntu20.04安装DeepStream6.1
    https://blog.csdn.net/m0_73702795/article/details/127940733    目录   前言   1.更新显卡驱动   2.下载CUDA   3.下载CUDNN   4.安装TensorRT   5.安装librdkafka   6.安装GStreamer   7.正式安装DeepStream前言本人亲自安装了十多遍......
  • ubuntu20.04 deepstream 6.3安装
    https://blog.csdn.net/zhoutianyou/article/details/135528185?spm=1001.2101.3001.6650.13&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-13-135528185-blog-127940733.235%5Ev43%5Epc_blog_bottom_relevance_base7&depth_1-utm......
  • Deepstream6.3部署YOLOv8
    https://blog.csdn.net/weixin_51230935/article/details/133296929?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-5-133296929-blog-135528185.235%5Ev43%5Epc_blog_bottom_relevance_base7&......
  • Pdf文件格式解析:stream中的变换矩阵指令 1 0 0 -1 0 841.9 cm
    解释100-10841.9cm在PDF文件中的变换矩阵指令100-10841.9cm中,前四个数值100-1组成了一个2x2的线性变换部分,用于描述旋转和缩放操作,而不涉及平移。这里,100-1的每一个数字都有特定的意义:第一个数字1:这是矩阵的第一行第一列的元素。它决定了x坐标在......
  • LeetCode in Python 88. Merge Sorted Array (合并两个有序数组)
    合并有序数组也有两种方法,区别是空间复杂度不同。第一种,重新开辟一个数组空间,大小为O(m+n),另外需要三个指针分别指向两个有序数组和新开辟的数组,依次判断两个数组内元素大小,不断更新指针即可。第二种,无需单独开辟空间,在第一个数组(该数组空间足够存放两个数组总长的数据)内进行......
  • 02-JDK新特性-Stream流
    Stream流什么是Stream流Stream流是Java8中的一个新特性,它提供了一种处理集合和数组的方式。Stream流可以让我们以一种更加简洁、高效、可读性更强的方式来处理数据。Stream流可以用于过滤、映射、排序、聚合等操作,它可以让我们避免使用循环和条件语句来处理数据,从而让代......
  • jdk8 StreamAPI 的使用
    本文将以UmsMenu对象为例来介绍StreamAPI的常用操作。UmsMenu是一个菜单对象,具有树形结构,对象定义如下Stream对象的创建Stream对象分为两种,一种串行的流对象,一种并行的流对象。1//menuList指所有菜单列表2//为集合创建串行流对象3Stream<UmsMenu>stream=menuLis......
  • Nginx 配置反向代理时出现 502 Bad Gateway (13_ Permission denied) while connectin
    我们在Nginx配置反向代理后,可能会出现如下报错:502BadGateway(13:Permissiondenied)whileconnectingtoupstreamSorry,thepageyouarelookingforiscurrentlyunavailable.<br/>Pleasetryagainlater.这些错误,一般是由SELinux引起的(https://stackoverf......