欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8如何從一個Stream中過濾null值

 更新時間:2022年05月06日 10:40:03   作者:wangmm0218  
這篇文章主要介紹了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)文章

最新評論