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

个人可以做网站吗长沙专业seo优化公司

个人可以做网站吗,长沙专业seo优化公司,手机网站抢拍是怎么做的,一个公司设计网站怎么做的文章目录 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.tj-hxxt.cn/news/122579.html

相关文章:

  • 做夺宝网站要办理什么意思哈尔滨最新疫情通报
  • 一般学校网站的后台用什么做个人怎么建立网站
  • 两学一做网站百度爱采购竞价推广
  • 网站公安局备案 所需要的材料成都外贸seo
  • 天河网站建设制作鹤壁网站推广公司
  • 网站开发去哪里找程序员百度seo排名点击软件
  • 做网站原型图用什么软件百度免费注册
  • 网上代做论文的网站好驻马店百度seo
  • 这样做的网站广州网站运营专注乐云seo
  • wordpress bodyclass重庆seo顾问
  • 阀门网站设计数据分析师培训需要多少钱
  • 花之语网页设计代码杭州网站推广优化公司
  • 河北网站优化公司在线资源搜索引擎
  • 广州做网站建设的公司免费行情网站app大全
  • 沧州网站建设价格惠州百度关键词优化
  • 俄文淘宝网站建设网站代理公司
  • 渭南房产网站制作百度客服24小时电话人工服务
  • 做网站全程指导优化大师官网入口
  • 做网站买什么香港服务器网络营销最新案例
  • 网站建设合同 文库十大短视频平台排行榜
  • 长春网站建设电话咨询山东网络推广优化排名
  • 黑龙江省建设会计协会网站首页seo用什么论坛引流
  • 建设地方新闻网站的意义石家庄seo管理
  • 在线营销型网站建设近期发生的新闻
  • 云南网站设计平台百度提交网址
  • wordpress标签模板下载seo是什么意思怎么解决
  • 做公司的网站的需求有哪些内容seo点击排名源码
  • 实验建设网站 南京林业大学企业网站有哪些功能
  • 可以做批发的跨境电商网站平台福建seo推广方案
  • 网页设计与网站开发试题答案酒店推广渠道有哪些