jdk8
新特性stream深受喜爱,平时使用比较多,其中有:
Map
collect2 =
list.stream().collect(Collectors.toMap(Book::getName, Book::getIdNO,(pre, after) -> pre));
现象如下:
package MainTest.stream;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamListToMap {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Book("master amber", 1987, "ASHJBYGSFDFG15"));
list.add(new Book("slaver amber", 1987, "IJFGNMKETRE58"));
list.add(new Book("master amber", 1999, "MYHGORPJFJ64"));
list.add(new Book("ssdas", 12414, null));
list.add(new Book("fkajh", 32423, null));
// 问题1:jdk8这种情况下,value为空会报错,jdk9解决了这个问题(key为null没有问题)
System.out.println("==============");
Map collect2 = list.stream().collect(Collectors.toMap(Book::getName, Book::getIdNo,(pre, after) -> pre));
for (String key : collect2.keySet()) {
System.out.println("key:"+key + "=======value:"+collect2.get(key));
}
}
public static class Book {
private String name;
private int year;
private String idNo;
Book(String name, int year, String idNo) {
this.name = name;
this.year = year;
this.idNo = idNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
}
}
问题:JDK8环境下,如果Book::getIdNo 为null的话会报空指针,即JDK8环境下的,该接口不支持value为null(key为null没这个问题)
R collect(Collector super T, A, R> collector);
该接口不支持valueMapper为null,JDK9解决了这个问题
解决方案:
1.升级JDK9
2.按需求可以过滤掉null的value
3.使用另一个collect接口
R collect(Supplier supplier,BiConsumer accumulator, BiConsumer combiner); Supplier
supplier :收集器返回的容器 BiConsumer
accumulator : 消费者,R参数是返回结果类型,? super T 一般值list内元素 BiConsumer
combiner:联合器,主要是并行流下使用(parallel),非并行流不使用改参数,但不能为null
package MainTest.stream;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamListToMap {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Book("master amber", 1987, "ASHJBYGSFDFG15"));
list.add(new Book("slaver amber", 1987, "IJFGNMKETRE58"));
list.add(new Book("master amber", 1999, "MYHGORPJFJ64"));
list.add(new Book("ssdas", 12414, null));
list.add(new Book("fkajh", 32423, null));
/*// 问题1:jdk8这种情况下,value为空会报错,jdk9解决了这个问题(key为null没有问题)
System.out.println("==============");
Map collect2 = list.stream().collect(Collectors.toMap(Book::getName, Book::getIdNo,(pre, after) -> pre));
for (String key : collect2.keySet()) {
System.out.println("key:"+key + "=======value:"+collect2.get(key));
}*/
// 问题1解决方案1
System.out.println("==============");
HashMap collect3 = list.stream().parallel().collect(HashMap::new, (n, v) -> n.put(v.getName(), v.getIdNo()), HashMap::putAll);
for (String key : collect3.keySet()) {
System.out.println("key:"+key + "=======value:"+collect3.get(key));
}
// 问题1解决方案2 过滤掉null的,看需求
System.out.println("==============");
HashMap collect4 = list.stream().filter(l-> Objects.nonNull(l) && Objects.nonNull(l.idNo)).collect(HashMap::new, (n, v) -> n.put(v.getName(), v.getIdNo()), HashMap::putAll);
for (String key : collect4.keySet()) {
System.out.println("key:"+key + "=======value:"+collect4.get(key));
}
}
public static class Book {
private String name;
private int year;
private String idNo;
Book(String name, int year, String idNo) {
this.name = name;
this.year = year;
this.idNo = idNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
}
}
JDK8的bug ,还是新特性功能的bug,并且在下个版本做了修复,特此记录!!希望能帮到你奥!