网站优化排名易下拉排名,农业网站平台建设方案,上海网站建设caiyiduo,百度最贵关键词排名文章目录 JDK8对List对象根据属性排序1. 被排序字段为null或者空时候报错2. 使用Stream流排序2.1 根据name升序2.2 根据name升序#xff0c;score降序 3. 使用Collections排序3.1 根据name升序3.2 根据name升序#xff0c;score降序 4. 完整的demo 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的排在前面
集合工具类Collections
Collections.sort(students1,Comparator.comparing(Student::getName,Comparator.nullsLast(String::compareTo)));stream流的方式
ListStudent 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) {ListStudent students new ArrayListStudent(){{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));}};ListStudent students1 getNameAsc(students);students1.forEach(System.out::println);}//按name升序null和空的name都以null处理排在最后private static ListStudent getNameAsc(ListStudent students) {ListStudent 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(id3, name张三A, score85.4)
Student(id2, name张三B, score67.5)
Student(id1, name张三C, score98.5)
Student(id6, name张三D, score95.2)
Student(id4, namenull, score79.6)
Student(id5, namenull, score84.8)2.2 根据name升序score降序
//按name升序按score降序null和空的name都以null处理排在最后private static ListStudent getNameAscAndScoreDesc(ListStudent students) {ListStudent 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(id3, name张三A, score85.4)
Student(id2, name张三B, score67.5)
Student(id1, name张三C, score98.5)
Student(id6, name张三D, score95.2)
Student(id5, namenull, score84.8)
Student(id4, namenull, score79.6)3. 使用Collections排序
3.1 根据name升序
public class ListSortDemo1 {public static void main(String[] args) {ListStudent students new ArrayListStudent(){{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));}};//ListStudent students1 getNameAsc(students);//ListStudent students1 getNameAscAndScoreDesc(students);ListStudent students1 getNameAsc1(students);students1.forEach(System.out::println);}private static ListStudent getNameAsc1(ListStudent students) {ListStudent 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(id3, name张三A, score85.4)
Student(id2, name张三B, score67.5)
Student(id1, name张三C, score98.5)
Student(id6, name张三D, score95.2)
Student(id4, namenull, score79.6)
Student(id5, namenull, score84.8)3.2 根据name升序score降序
private static ListStudent getNameAscAndScoreDesc1(ListStudent students) {ListStudent 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(id3, name张三A, score85.4)
Student(id2, name张三B, score67.5)
Student(id1, name张三C, score98.5)
Student(id6, name张三D, score95.2)
Student(id5, namenull, score84.8)
Student(id4, namenull, score79.6)4. 完整的demo
public class ListSortDemo1 {public static void main(String[] args) {ListStudent students new ArrayListStudent(){{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));}};//ListStudent students1 getNameAsc(students);//ListStudent students1 getNameAscAndScoreDesc(students);//ListStudent students1 getNameAsc1(students);ListStudent students1 getNameAscAndScoreDesc(students);students1.forEach(System.out::println);}//按name升序null和空的name都以null处理排在最后private static ListStudent getNameAsc(ListStudent students) {ListStudent 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 ListStudent getNameAsc1(ListStudent students) {ListStudent 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 ListStudent getNameAscAndScoreDesc(ListStudent students) {ListStudent 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 ListStudent getNameAscAndScoreDesc1(ListStudent students) {ListStudent 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.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.xxhc.cn.gov.cn.xxhc.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.ydhck.cn.gov.cn.ydhck.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.yrctp.cn.gov.cn.yrctp.cn http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.rsdm.cn.gov.cn.rsdm.cn