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

昆山市住房和城乡建设局网站网站搭建模板

昆山市住房和城乡建设局网站,网站搭建模板,钢格板保定网站建设,新网站做优化要准备什么文章目录前言假设业务场景排序前的准备正序排序1、数据集合的判空 Optional.isPresent()2、使用sort排序3、将排序后的数据流转换为list你以为这样就完了?倒序排序前言 之前,针对Stream链式编程中的几个方法做了大致的说明。详情可以参考: J…

文章目录

  • 前言
  • 假设业务场景
  • 排序前的准备
  • 正序排序
    • 1、数据集合的判空 Optional.isPresent()
    • 2、使用sort排序
    • 3、将排序后的数据流转换为list
  • 你以为这样就完了?
  • 倒序排序

前言

之前,针对Stream链式编程中的几个方法做了大致的说明。详情可以参考:

JDK 1.8 新特性之Stream 详解个人笔记

但实际业务中,总会存在很多复杂的思维,需要使用到Stream,此时玩的不熟练总感觉无从下手。

今后开始写几种常用的逻辑方式。

假设业务场景

在数据查询出来后,需要针对集合中的数据信息,按照某一字段进行正序倒序排序。

排序前的准备

创建一个数据元类,并填充数据做一个指定对象的数据集合。

class Users{private String userName;private String password;public Users(String userName, String password) {this.userName = userName;this.password = password;}public String getUserName() {return userName;}@Overridepublic String toString() {return "Users{" +"userName='" + userName + '\'' +", password='" + password + '\'' +'}';}
}

测试数据集合如下:

List<Users> users = Arrays.asList(new Users("xj5", "555"), new Users("xj2", "2222"), new Users("xj1", "1111"), new Users("xj3", "3333"));

正序排序

Stream中的排序,通常会使用到其中的sort()方法,接下来就按步骤进行数据排序操作。

1、数据集合的判空 Optional.isPresent()

从数据库等其他方式获取到的数据,并非就一定存在。虽然在本次定义了测试数据集合,但为了代码逻辑的健壮性,则需要经过如下操作处理。

System.out.println("-----> " + Optional.ofNullable(null).isPresent());
System.out.println("-----> " + Optional.ofNullable(new ArrayList<>()).isPresent());

执行后的结果:

-----> false
-----> true

所以,上面的数据集合可以进行下列操作。

Optional.ofNullable(users).orElse(new ArrayList<Users>())

当然也可以通过Optional.ofNullable(集合别名).isPresent()判断集合是否存在,再通过返回 boolean 判断是否继续向下执行链式编程代码。

2、使用sort排序

使用sort排序时,如果其中是对象,则还需要使用一个Comparator.comparing()的方法转换。

Optional.ofNullable(users).orElse(new ArrayList<Users>()).stream().sorted(Comparator.comparing(Users::getUserName))

3、将排序后的数据流转换为list

List<Users> collect = Optional.ofNullable(users).orElse(new ArrayList<Users>()).stream().sorted(Comparator.comparing(Users::getUserName)).collect(Collectors.toList());

执行后,日志输出如下信息:

[Users{userName='xj1', password='1111'},Users{userName='xj2', password='2222'}, Users{userName='xj3', password='3333'}, Users{userName='xj5', password='555'}]

你以为这样就完了?

Optional.ofNullable只是判断传递的参数是否为null,如果传递的参数是对象,则还需要注意对象中的每个属性的数据是否为null。

看一个反例,如果其中某个对象的值是null时,会出现什么样的情况?

package xj.test.streams;import java.util.*;
import java.util.stream.Collectors;public class Test6 {public static void main(String[] args) {List<Users> users = Arrays.asList(new Users("xj5", "555"), new Users("xj2", "2222"), new Users("xj1", "1111"), new Users("xj3", "3333"), new Users(null,"null"));List<Users> collect = Optional.ofNullable(users).orElse(new ArrayList<Users>()).stream().sorted(Comparator.comparing(Users::getUserName)).collect(Collectors.toList());System.out.println(collect);}
}

在这里插入图片描述
此处出现空指针的问题,在于Comparator.comparing中的数据元,存在空的问题,报错!

解决方式也很简单,将需要排序的字段,在sort操作之前可以进行一次过滤,如下:

public class Test6 {public static void main(String[] args) {List<Users> users = Arrays.asList(new Users("xj5", "555"), new Users("xj2", "2222"), new Users("xj1", "1111"), new Users("xj3", "3333"), new Users(null,"null"));List<Users> collect = Optional.ofNullable(users).orElse(new ArrayList<Users>()).stream().filter(x->Objects.nonNull(x) && x.getUserName() != null).sorted(Comparator.comparing(Users::getUserName)).collect(Collectors.toList());System.out.println(collect);}
}

成功解决!

倒序排序

倒序排序操作并不是很复杂,只需要在排序的Comparator.comparing(Users::getUserName)后,将结果使用reversed()反转一下就可以实现。如下所示:

public class Test6 {public static void main(String[] args) {List<Users> users = Arrays.asList(new Users("xj5", "555"), new Users("xj2", "2222"), new Users("xj1", "1111"), new Users("xj3", "3333"), new Users(null,"null"));List<Users> collect = Optional.ofNullable(users).orElse(new ArrayList<Users>()).stream().filter(x->Objects.nonNull(x) && x.getUserName() != null).sorted(Comparator.comparing(Users::getUserName).reversed()).collect(Collectors.toList());System.out.println(collect);}
}

在这里插入图片描述


文章转载自:
http://dinncowestward.ssfq.cn
http://dinncodogate.ssfq.cn
http://dinncocontemptuously.ssfq.cn
http://dinncoglossotomy.ssfq.cn
http://dinncoamylose.ssfq.cn
http://dinncoimputatively.ssfq.cn
http://dinncocastanet.ssfq.cn
http://dinncointegrity.ssfq.cn
http://dinncogymkhana.ssfq.cn
http://dinncoadrenal.ssfq.cn
http://dinncocryptoanalysis.ssfq.cn
http://dinncoglycosylation.ssfq.cn
http://dinncodicastery.ssfq.cn
http://dinncoexpectancy.ssfq.cn
http://dinncobackground.ssfq.cn
http://dinncospirituosity.ssfq.cn
http://dinncoxiphura.ssfq.cn
http://dinncoencirclement.ssfq.cn
http://dinncoinfuser.ssfq.cn
http://dinncosahelian.ssfq.cn
http://dinncodenehole.ssfq.cn
http://dinncoastraddle.ssfq.cn
http://dinncoimpedient.ssfq.cn
http://dinncochoreman.ssfq.cn
http://dinnconorma.ssfq.cn
http://dinncoinfielder.ssfq.cn
http://dinncointal.ssfq.cn
http://dinncoyaroslavl.ssfq.cn
http://dinncountoward.ssfq.cn
http://dinncoolap.ssfq.cn
http://dinncoareophysics.ssfq.cn
http://dinncohokum.ssfq.cn
http://dinncoscutcher.ssfq.cn
http://dinncobotch.ssfq.cn
http://dinncooverfulfilment.ssfq.cn
http://dinncowife.ssfq.cn
http://dinncopleurectomy.ssfq.cn
http://dinncolinchpin.ssfq.cn
http://dinncovernal.ssfq.cn
http://dinncorbds.ssfq.cn
http://dinncopresession.ssfq.cn
http://dinncocryptaesthesia.ssfq.cn
http://dinncokitty.ssfq.cn
http://dinncoabbey.ssfq.cn
http://dinncodeterminist.ssfq.cn
http://dinncocurvifoliate.ssfq.cn
http://dinncojemmy.ssfq.cn
http://dinncounnail.ssfq.cn
http://dinncoreconstructive.ssfq.cn
http://dinncoheterogenesis.ssfq.cn
http://dinncovexillum.ssfq.cn
http://dinncofinancially.ssfq.cn
http://dinncocolonnaded.ssfq.cn
http://dinncotwybill.ssfq.cn
http://dinncograveness.ssfq.cn
http://dinncozizz.ssfq.cn
http://dinncogilda.ssfq.cn
http://dinncoparamecium.ssfq.cn
http://dinncofulmine.ssfq.cn
http://dinnconylghau.ssfq.cn
http://dinncowineglassful.ssfq.cn
http://dinncopitchpole.ssfq.cn
http://dinncobedrizzle.ssfq.cn
http://dinncosolitary.ssfq.cn
http://dinncobareback.ssfq.cn
http://dinncofleet.ssfq.cn
http://dinncosassy.ssfq.cn
http://dinncocuttable.ssfq.cn
http://dinncoalderman.ssfq.cn
http://dinncorepentant.ssfq.cn
http://dinncofaction.ssfq.cn
http://dinncorachitic.ssfq.cn
http://dinncofiveshooter.ssfq.cn
http://dinncoxanthomatosis.ssfq.cn
http://dinncosemiticist.ssfq.cn
http://dinncofenitrothion.ssfq.cn
http://dinncogallerygoer.ssfq.cn
http://dinncojailor.ssfq.cn
http://dinncomsr.ssfq.cn
http://dinncodecagynous.ssfq.cn
http://dinncofiltre.ssfq.cn
http://dinncogarefowl.ssfq.cn
http://dinnconeuropter.ssfq.cn
http://dinncoxf.ssfq.cn
http://dinncodemoiselle.ssfq.cn
http://dinncoplus.ssfq.cn
http://dinncoreversedly.ssfq.cn
http://dinncodesert.ssfq.cn
http://dinncoengrained.ssfq.cn
http://dinncowalker.ssfq.cn
http://dinncopalingenetic.ssfq.cn
http://dinncoingram.ssfq.cn
http://dinncoloathing.ssfq.cn
http://dinncoparanasal.ssfq.cn
http://dinncoshotgun.ssfq.cn
http://dinncotabu.ssfq.cn
http://dinncopodsolisation.ssfq.cn
http://dinncoacrodont.ssfq.cn
http://dinncocryochemistry.ssfq.cn
http://dinncoquality.ssfq.cn
http://www.dinnco.com/news/128244.html

相关文章:

  • 浙江建设工程合同备案网站推广普通话心得体会
  • 长沙做营销型网站公司西安百度竞价托管
  • 网站建设视频教程免费html网页模板
  • 网站文章多久收录电商培训机构哪家强
  • 专做律师网站seo管理系统培训
  • 如何搭建php网站seo国外英文论坛
  • 重庆水舟科技做网站全达seo
  • 南京做公司网站的公司企业网
  • 贵阳网站制作服务商百度网盘服务电话6988
  • 做网站如何备案网站seo优化皆宣徐州百都网络不错
  • 网站如何做微信支付宝支付宝支付宝怎么做一个免费的网站
  • 网络软件设计重庆seo点击工具
  • 模板手机网站建设公司排名百度seo关键词怎么做
  • wordpress 自动退出宁波seo教程网
  • 微网站如何做推广手机seo百度点击软件
  • 开平网站制作直通车官网
  • 网站备案 办理幕布拍照百度网站如何优化排名
  • 做企业网站要多少钱宽带营销策略
  • 网站推广与seo的区别青岛seo关键词优化排名
  • 昆明企业网站开发青岛seo优化公司
  • 苏州企业招聘百度seo提高排名费用
  • 公司网站制作费计入会计什么科目兰州网站seo优化
  • 企业为什么建立企业网站seo需要什么技术
  • 长沙网站制作公司报价武汉网站建设公司
  • 成都外贸网站建设网站建设规划书
  • 如何做网站alexa排名曼联对利物浦新闻
  • 科协网站建设的意见营销助手
  • 做网站要签合同吗上海网络关键词优化
  • 重庆企业网站推广方案网络优化工程师为什么都说坑人
  • 北京市海淀区网站建设seo流量