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

网站建设意向书没有网站怎么做百度竞价

网站建设意向书,没有网站怎么做百度竞价,滁州网络推广公司,临沂谁会做网站文章目录 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对象根据属性排序…

文章目录

  • 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.yayakq.cn/news/152766/

相关文章:

  • 大型网站制作流程网站备案需要那些资料
  • fwa 网站 欣赏网页版qq在线登录
  • 甘肃省酒泉市做网站公司个人网店搭建思路设计
  • 辽宁平台网站建设价位做网站那个程序好
  • 广西长长路桥建设有限公司网站做网站设计师的感想
  • 培训会网站建设昆明网络推广招聘
  • 手机的网站建设目标是什么意思科技是第一生产力人才是第一资源创新是第一动力判断题
  • 做汽车团购的网站有哪些高端建设网站公司
  • 系统开发网站沈阳seo网站推广
  • 画家网站建设博罗网站设计
  • 鞍山制作网站哪家好广西网络推广怎么做
  • 厦门营销网站建设公司科技创新的重要性和意义
  • 移动端网站建设推广方案网站照片要求
  • 普通网站做网站开发风险协议
  • 濮阳市网站建设公司软件开发制作流程图
  • 网站的ftp地址怎么查自建站工具
  • 深圳国税局网站怎么做票种核定wordpress下载的插件怎么用
  • 企业还做网站吗手机免费制图软件
  • 科技网站配色方案网站建设商家公司
  • 长沙商城网站开发wordpress 开启链接
  • 做销售用什么网站好飘雪影院手机免费观看免费
  • 集团公司中英文网站模板壹舍设计公司
  • 学做网站论坛 可以吗中国网站设计师
  • 网站后台用什么开发网络营销方式研究心得1500
  • 免费制作论坛网站模板ppt免费下载雷锋网站
  • 平邑做网站的北京代理记账公司电话
  • 寿县城乡建设局网站在微信上做彩票网站有哪些
  • 那些彩票广告网站怎么做的做电影网站资源怎么
  • 北京市住房城乡建设门户网站做网站大公司有哪些
  • 在网站做电子画册做产品表情的网站