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(Collectorpackage 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解决方案1System.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,并且在下个版本做了修复,特此记录!!希望能帮到你奥!