Java8如何從一個Stream中過濾null值
從一個Stream中過濾null值
復(fù)習一個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為例
實際上只要是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 Cloud Config結(jié)合Bus實現(xiàn)分布式配置中心的步驟
這篇文章主要介紹了利用Spring Cloud Config結(jié)合Bus實現(xiàn)分布式配置中心的相關(guān)資料,文中通過示例代碼將實現(xiàn)的步驟一步步介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧2018-05-05Java按照List內(nèi)存儲的對象的某個字段進行排序的實例
下面小編就為大家?guī)硪黄狫ava按照List內(nèi)存儲的對象的某個字段進行排序的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12mall整合SpringSecurity及JWT實現(xiàn)認證授權(quán)實戰(zhàn)
這篇文章主要為大家介紹了mall整合SpringSecurity及JWT實現(xiàn)認證授權(quán)實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06如何在springMVC的controller中獲取request
這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題
這篇文章主要介紹了解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06