Java8如何從一個Stream中過濾null值
從一個Stream中過濾null值
復(fù)習(xí)一個Stream 包含 null 數(shù)據(jù)的例子.
Java8Examples.java
package com.mkyong.java8;?
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
?
public class Java8Examples {?
? ? public static void main(String[] args) {?
? ? ? ? Stream<String> language = Stream.of("java", "python", "node", null, "ruby", null, "php");?
? ? ? ? List<String> result = 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
package com.mkyong.java8;?
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
?
public class Java8Examples {?
? ? public static void main(String[] args) {?
? ? ? ? Stream<String> language = Stream.of("java", "python", "node", null, "ruby", null, "php");?
? ? ? ? //List<String> result = language.collect(Collectors.toList());?
? ? ? ? List<String> result = language.filter(x -> x!=null).collect(Collectors.toList());?
? ? ? ? result.forEach(System.out::println); ?
? ? }
}output
java
python
node
ruby
php
另外,過濾器還可以用: Objects::nonNull
import java.util.List; List<String> result = language.filter(Objects::nonNull).collect(Collectors.toList());
stream方法過濾條件的使用
@Data
@AllArgsConstructor
public class User {
private Long id; // id
private Integer age; // 年齡
private Byte gentle; // 性別
private String name; // 名字
private Integer rank; // 排名
}
User user0 = new User(1L, 18, (byte) 0, "張三", 1);
User user1 = new User(2L, 20, (byte) 1, "李四", 4);
User user2 = new User(3L, 35, (byte) 0, "王五", 2);
User user3 = new User(4L, 29, (byte) 1, "趙六", 3);下面以List為例
實(shí)際上只要是Collection的子類,玩法都類似
1、生成stream
List<User> list = Arrays.asList(user0, user1, user2, user3); Stream<User> stream = null; stream = list.stream(); // 需要預(yù)判NPE stream = Optional.of(list).orElseGet(Collections::emptyList).stream(); // 需要預(yù)判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(); // 直接構(gòu)造 stream = Stream.of(Arrays.asList(user0, user1), Arrays.asList(user2, user3)).flatMap(Collection::stream); // flatMap合并
2、stream操作
// 過濾出性別為0的user
List<User> userList = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().filter(user -> (byte) 0 == user.getGentle()).collect(Collectors.toList());
// 獲取排名大于1的用戶年齡set
Set<Integer> ageList = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().filter(user -> 1 < user.getRank()).map(User::getAge).collect(Collectors.toSet());
// 合計性別為0的user的年齡
Integer totalAge = Optional.ofNullable(userList).orElseGet(Collections::emptyList).stream().map(User::getAge).reduce(0, Integer::sum);
// 按排名倒序排列
List<User> sortedUserList = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().sorted(Comparator.comparing(User::getRank, Comparator.reverseOrder())).collect(Collectors.toList());
// 獲取排名第2高的user
User rankUser = Optional.ofNullable(sortedUserList).orElseGet(Collections::emptyList).stream().skip(1).findFirst().get();
// 排名最高的user
User highestRankUser = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().max(Comparator.comparing(User::getRank)).get();
// 是否存在排名大于1的user
boolean flag = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().anyMatch(user -> user.getRank() > 1);
// 是否所有user排名都大于1
boolean flag = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().allMatch(user -> user.getRank() > 1);
// 是否所有user排名都不大于5
boolean flag = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().noneMatch(user -> user.getRank() > 5);
// 按唯一id分組
Map<Long, User> idUserMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getId, Function.identity()));
// 按唯一id,名字分組
Map<Long, String> idNameMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getId, User::getName));
// 按年齡,名字分組,相同年齡的后出現(xiàn)的被覆蓋
Map<Integer, String> ageNameMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.toMap(User::getAge, User::getName, (a, b) -> a));
// 按性別分組
Map<Byte, List<User>> gentleUserMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(User::getGentle));
// 按排名是否大于3分組
Map<Boolean, List<User>> partitionUserMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.partitioningBy(user -> user.getRank() > 3));
// 按性別名字分組
Map<Byte, List<String>> gentleNameMap = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(User::getGentle, Collectors.mapping(User::getName, Collectors.toList())));
// 按性別年齡總和分組
Map<Byte, Integer> gentleTotalAgeMap = 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());
});
// guava table轉(zhuǎn)換
Table<Long, String, Integer> idNameRankTable = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream().map(user -> ImmutableTable.of(user.getId(), user.getName(), user.getRank())).collect(HashBasedTable::create, HashBasedTable::putAll, HashBasedTable::putAll);
// stream只能被terminal一次,下面是錯誤示范 Stream<User> stream = Optional.ofNullable(list).orElseGet(Collections::emptyList).stream(); stream.collect(Collectors.toMap(User::getId, Function.identity())); stream.collect(Collectors.toMap(User::getId, User::getName)); // java.lang.IllegalStateException: stream has already been operated upon or closed // ssc-common的com.meicloud.mcu.common.util.StreamUtil簡單封裝了一些流操作,歡迎試用 // 參考資料:https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個關(guān)鍵的接口,用于創(chuàng)建和管理對象實(shí)例,它們在Spring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
SpringBoot集成本地緩存性能之王Caffeine示例詳解
這篇文章主要為大家介紹了SpringBoot集成本地緩存性能之王Caffeine的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
實(shí)例詳解Spring Boot實(shí)戰(zhàn)之Redis緩存登錄驗證碼
本章簡單介紹redis的配置及使用方法,本文示例代碼在前面代碼的基礎(chǔ)上進(jìn)行修改添加,實(shí)現(xiàn)了使用redis進(jìn)行緩存驗證碼,以及校驗驗證碼的過程。感興趣的的朋友一起看看吧2017-08-08
springboot統(tǒng)一接口返回數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了springboot統(tǒng)一接口返回數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊列方案及使用場景
在很多的業(yè)務(wù)場景中,延時隊列可以實(shí)現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實(shí)時的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?這篇文章主要介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊列方案及使用場景,需要的朋友可以參考下2024-04-04
java并發(fā)數(shù)據(jù)包Exchanger線程間的數(shù)據(jù)交換器
這篇文章主要為大家介紹了java并發(fā)數(shù)據(jù)包使用數(shù)據(jù)交換器Exchanger來進(jìn)行線程之間的數(shù)據(jù)交換。有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03
SpringBoot攔截器實(shí)現(xiàn)登錄攔截的示例代碼
本文主要介紹了SpringBoot攔截器實(shí)現(xiàn)登錄攔截,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

