当前位置: 首页 > news >正文

JDK8对List对象根据属性排序

文章目录

  • JDK8对List对象根据属性排序
  • 1. 被排序字段为null或者空时候报错
  • 2. 使用Stream流排序
    • 2.1 根据name升序
    • 2.2 根据name升序,score降序
  • 3. 使用Collections排序
    • 3.1 根据name升序
    • 3.2 根据name升序,score降序
  • 4. 完整的demo

JDK8对List对象根据属性排序

1. 被排序字段为null或者空时候报错

被排序字段为null或者空的时候报java.lang.NullPointerException

Exception in thread "main" java.lang.NullPointerExceptionat java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356)at java.util.TimSort.sort(TimSort.java:220)at java.util.Arrays.sort(Arrays.java:1512)at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:348)at java.util.stream.Sink$ChainedReference.end(Sink.java:258)at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)at com.stormkai.jh.ListSortDemo1.getNameAsc(ListSortDemo1.java:40)at com.stormkai.jh.ListSortDemo1.main(ListSortDemo1.java:19)

使用以下方式处理:

  • Comparator.nullsLast:排序字段为null的排在后面
  • Comparator.nullsFirst:排序字段为null的排在前面
  1. 集合工具类Collections
Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)));
  1. stream流的方式
List<Student> students1 = students.stream().map(student -> {if("".equals(student.getName())){student.setName(null);}return student;}).sorted(Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList())

2. 使用Stream流排序

Student.java

@Data
@AllArgsConstructor
public class Student {private Integer id;private String name;private double score;
}

2.1 根据name升序

public class ListSortDemo1 {public static void main(String[] args) {List<Student> students = new ArrayList<Student>(){{add(new Student(1,"张三C",98.50d));add(new Student(2,"张三B",67.50d));add(new Student(3,"张三A",85.40d));add(new Student(4,"",79.60d));add(new Student(5,null,84.80d));add(new Student(6,"张三D",95.20d));}};List<Student> students1 = getNameAsc(students);students1.forEach(System.out::println);}//按name升序,null和空的name都以null处理,排在最后private static List<Student> getNameAsc(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if("".equals(student.getName())){student.setName(null);}return student;}).sorted(Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());return students1;}
}

执行结果:

Student(id=3, name=张三A, score=85.4)
Student(id=2, name=张三B, score=67.5)
Student(id=1, name=张三C, score=98.5)
Student(id=6, name=张三D, score=95.2)
Student(id=4, name=null, score=79.6)
Student(id=5, name=null, score=84.8)

2.2 根据name升序,score降序

//按name升序,按score降序,null和空的name都以null处理,排在最后private static List<Student> getNameAscAndScoreDesc(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if("".equals(student.getName())){student.setName(null);}return student;}).sorted(Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)).reversed().thenComparing(Student::getScore).reversed()).collect(Collectors.toList());return students1;}

输出结果:

Student(id=3, name=张三A, score=85.4)
Student(id=2, name=张三B, score=67.5)
Student(id=1, name=张三C, score=98.5)
Student(id=6, name=张三D, score=95.2)
Student(id=5, name=null, score=84.8)
Student(id=4, name=null, score=79.6)

3. 使用Collections排序

3.1 根据name升序

public class ListSortDemo1 {public static void main(String[] args) {List<Student> students = new ArrayList<Student>(){{add(new Student(1,"张三C",98.50d));add(new Student(2,"张三B",67.50d));add(new Student(3,"张三A",85.40d));add(new Student(4,"",79.60d));add(new Student(5,null,84.80d));add(new Student(6,"张三D",95.20d));}};//List<Student> students1 = getNameAsc(students);//List<Student> students1 = getNameAscAndScoreDesc(students);List<Student> students1 = getNameAsc1(students);students1.forEach(System.out::println);}private static List<Student> getNameAsc1(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if ("".equals(student.getName())) {student.setName(null);}return student;}).collect(Collectors.toList());Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)));return students1;}
}

输出结果:

Student(id=3, name=张三A, score=85.4)
Student(id=2, name=张三B, score=67.5)
Student(id=1, name=张三C, score=98.5)
Student(id=6, name=张三D, score=95.2)
Student(id=4, name=null, score=79.6)
Student(id=5, name=null, score=84.8)

3.2 根据name升序,score降序

private static List<Student> getNameAscAndScoreDesc1(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if ("".equals(student.getName())) {student.setName(null);}return student;}).collect(Collectors.toList());Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)).reversed().thenComparing(Student::getScore).reversed());return students1;}

输出结果:

Student(id=3, name=张三A, score=85.4)
Student(id=2, name=张三B, score=67.5)
Student(id=1, name=张三C, score=98.5)
Student(id=6, name=张三D, score=95.2)
Student(id=5, name=null, score=84.8)
Student(id=4, name=null, score=79.6)

4. 完整的demo

public class ListSortDemo1 {public static void main(String[] args) {List<Student> students = new ArrayList<Student>(){{add(new Student(1,"张三C",98.50d));add(new Student(2,"张三B",67.50d));add(new Student(3,"张三A",85.40d));add(new Student(4,"",79.60d));add(new Student(5,null,84.80d));add(new Student(6,"张三D",95.20d));}};//List<Student> students1 = getNameAsc(students);//List<Student> students1 = getNameAscAndScoreDesc(students);//List<Student> students1 = getNameAsc1(students);List<Student> students1 = getNameAscAndScoreDesc(students);students1.forEach(System.out::println);}//按name升序,null和空的name都以null处理,排在最后private static List<Student> getNameAsc(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if("".equals(student.getName())){student.setName(null);}return student;}).sorted(Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo))).collect(Collectors.toList());return students1;}private static List<Student> getNameAsc1(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if ("".equals(student.getName())) {student.setName(null);}return student;}).collect(Collectors.toList());Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)));return students1;}//按name升序,按score降序,null和空的name都以null处理,排在最后private static List<Student> getNameAscAndScoreDesc(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if("".equals(student.getName())){student.setName(null);}return student;}).sorted(Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)).reversed().thenComparing(Student::getScore).reversed()).collect(Collectors.toList());return students1;}private static List<Student> getNameAscAndScoreDesc1(List<Student> students) {List<Student> students1 = students.stream().map(student -> {if ("".equals(student.getName())) {student.setName(null);}return student;}).collect(Collectors.toList());Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)).reversed().thenComparing(Student::getScore).reversed());return students1;}
}
http://www.lryc.cn/news/292393.html

相关文章:

  • 【2024美国大学生数学建模竞赛】2024美赛C题网球运动中的势头,网球教练4.0没人比我更懂这个题了!!!
  • python的Flask生产环境部署说明照做成功
  • EXCEL VBA调用百度api识别身份证
  • 【每日一题】7.LeetCode——合并两个有序链表
  • 【零基础学习CAPL】——CAN报文的发送(按下按钮同时周期性发送)
  • 六、Nacos源码系列:Nacos健康检查
  • 2024美赛C题思路/代码:网球中的动量
  • ConcurrentHashMap原理详解(太细了)
  • EasyExcel根据对应的实体类模板完成多个sheet的写入与读取
  • 在企业数字化转型过程中,IT运维发挥着怎样的价值?
  • 01-工厂模式 ( Factory Pattern )
  • 【LeetCode】每日一题 2024_2_2 石子游戏 VI(排序、贪心)
  • 一站式在线协作开源办公软件ONLYOFFICE,协作更安全更便捷
  • Java进击框架:Spring-综合(十)
  • 2024年第九届信号与图像处理国际会议(ICSIP 2024)
  • webassembly003 MINISIT mnist/convert-h5-to-ggml.py
  • fetch和axios的区别
  • 【unity小技巧】FPS简单的射击换挡瞄准动画控制
  • 如何获取时间戳
  • VSCode 设置代理
  • 保姆级教程: 零门槛制作AI微信红包封面之入门篇
  • Redis核心技术与实战【学习笔记】 - 17.Redis 缓存异常:缓存雪崩、击穿、穿透
  • Leetcode—2670. 找出不同元素数目差数组【简单】
  • App ICP备案获取iOS和Android的公钥和证书指纹
  • 猿创征文 | 项目整合KafkaStream实现文章热度实时计算
  • 状态压缩 笔记
  • Java 数据结构篇-实现二叉搜索树的核心方法
  • go语言(二十一)---- channel的关闭
  • 【PyQt】01-PyQt下载
  • 不一样的味觉体验:精酿啤酒与烤肉的绝妙搭配