网站课程建设申报书旧电脑做网站服务器

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

网站课程建设申报书,旧电脑做网站服务器,建网上商城的第三方网站哪个好,眉山建设中等职业技术学校 网站文章目录 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;} }