




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Java8如何从一个Stream中过滤null值目录从一个Stream中过滤null值Solution(解决)stream方法过滤条件的使用下面以List为例
从一个Stream中过滤null值
复习一个Stream包含null数据的例子.
Java8Examples.java
packagecom.mkyong.java8;
importjava.util.List;
importjava.util.stream.Collectors;
importjava.util.stream.Stream;
publicclassJava8Examples{
publicstaticvoidmain(String[]args){
StreamStringlanguage=Stream.of("java","python","node",null,"ruby",null,"php");
ListStringresult=language.collect(Collectors.toList());
result.forEach(System.out::println);
}
}
output
java
python
node
null//---NULL
ruby
null//---NULL
php
Solution(解决)
为了解决上面的问题,我们使用:Stream.filter(x-x!=null)
Java8Examples.java
packagecom.mkyong.java8;
importjava.util.List;
importjava.util.stream.Collectors;
importjava.util.stream.Stream;
publicclassJava8Examples{
publicstaticvoidmain(String[]args){
StreamStringlanguage=Stream.of("java","python","node",null,"ruby",null,"php");
//ListStringresult=language.collect(Collectors.toList());
ListStringresult=language.filter(x-x!=null).collect(Collectors.toList());
result.forEach(System.out::println);
}
}
output
java
python
node
ruby
php
另外,过滤器还可以用:Objects::nonNull
importjava.util.List;
ListStringresult=language.filter(Objects::nonNull).collect(Collectors.toList());
stream方法过滤条件的使用
@Data
@AllArgsConstructor
publicclassUser{
privateLongid;//id
privateIntegerage;//年龄
privateBytegentle;//性别
privateStringname;//名字
privateIntegerrank;//排名
Useruser0=newUser(1L,18,(byte)0,"张三",1);
Useruser1=newUser(2L,20,(byte)1,"李四",4);
Useruser2=newUser(3L,35,(byte)0,"王五",2);
Useruser3=newUser(4L,29,(byte)1,"赵六",3);
下面以List为例
实际上只要是Collection的子类,玩法都类似
1、生成stream
ListUserlist=Arrays.asList(user0,user1,user2,user3);
StreamUserstream=null;
stream=list.stream();//需要预判NPE
stream=Optional.of(list).orElseGet(Collections::emptyList).stream();//需要预判NPE
stream=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream();
stream=Optional.ofNullable(list).orElseGet(Collections::emptyList).parallelStream();//并行处理流
stream=Stream.of(user0,user1,user2,user3).parallel();//直接构造
stream=Stream.of(Arrays.asList(user0,user1),Arrays.asList(user2,user3)).flatMap(Collection::stream);//flatMap合并
2、stream操作
//过滤出性别为0的user
ListUseruserList=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().filter(user-(byte)0==user.getGentle()).collect(Collectors.toList());
//获取排名大于1的用户年龄set
SetIntegerageList=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().filter(user-1user.getRank()).map(User::getAge).collect(Collectors.toSet());
//合计性别为0的user的年龄
IntegertotalAge=Optional.ofNullable(userList).orElseGet(Collections::emptyList).stream().map(User::getAge).reduce(0,Integer::sum);
//按排名倒序排列
ListUsersortedUserList=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().sorted(Cparing(User::getRank,Comparator.reverseOrder())).collect(Collectors.toList());
//获取排名第2高的user
UserrankUser=Optional.ofNullable(sortedUserList).orElseGet(Collections::emptyList).stream().skip(1).findFirst().get();
//排名最高的user
UserhighestRankUser=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().max(Cparing(User::getRank)).get();
//是否存在排名大于1的user
booleanflag=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().anyMatch(user-user.getRank()
//是否所有user排名都大于1
booleanflag=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().allMatch(user-user.getRank()
//是否所有user排名都不大于5
booleanflag=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().noneMatch(user-user.getRank()
//按唯一id分组
MapLong,UseridUserMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getId,Function.identity()));
//按唯一id,名字分组
MapLong,StringidNameMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getId,User::getName));
//按年龄,名字分组,相同年龄的后出现的被覆盖
MapInteger,StringageNameMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getAge,User::getName,(a,b)-a));
//按性别分组
MapByte,ListUsergentleUserMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(User::getGentle));
//按排名是否大于3分组
MapBoolean,ListUserpartitionUserMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.partitioningBy(user-user.getRank()3));
//按性别名字分组
MapByte,ListStringgentleNameMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(User::getGentle,Collectors.mapping(User::getName,Collectors.toList())));
//按性别年龄总和分组
MapByte,IntegergentleTotalAgeMap=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(User::getGentle,Collectors.reducing(0,User::getAge,Integer::sum)));
//迭代操作
Stream.iterate(0,i-i+1).limit(list.size()).forEach(i-{
System.out.println(list.get(i).getName());
//guavatable转换
TableLong,String,IntegeridNameRankTable=Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().map(user-ImmutableTable.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 健康轻食吧承包经营及特色菜品开发合同
- 茶楼招牌设计与制作及整体装修合同
- 智能温室彩钢大棚设计与施工总承包合同
- 车辆装饰升级方案
- 新安全法考试题及答案
- 商业街区场地使用权置换及合作开发合同
- 库存控制模型课件
- 医疗物资启运方案
- 电气维护培训课件
- 公园噪音改造方案
- 2025年湖南省高考物理试卷真题(含答案解析)
- 2025年中国东航旗下东方航空食品投资有限公司招聘笔试参考题库含答案解析
- 农业肥料溯源管理制度
- T/CCS 076-2023煤矿井下钻孔浆体充填技术要求
- 唾液腺肿瘤护理
- 行走的医院乡村巡回医疗健康服务规范
- 2025年山东高考考试说明(各科均有)
- 部队禁酒课件
- 2024年山西杏花村汾酒集团有限责任公司招聘真题
- 后期入股合同协议
- 【信得科技】2025猪腹泻病防控手册
评论
0/150
提交评论