共计 4254 个字符,预计需要花费 11 分钟才能阅读完成。
当 List 中有 null 值的时候,应用 Collectors.toMap() 转为 Map 时,会报 java.lang.NullPointerException,如下:
复制代码
List<SdsTest> sdsTests = new ArrayList<>();
SdsTest sds1 = new SdsTest("aaa","aaa");
SdsTest sds2 = new SdsTest("bbb",null);
sdsTests.add(sds1);
sdsTests.add(sds2);
Map<String, String> map = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, SdsTest::getAge));
System.out.println(map.toString());
运行谬误:
Exception in thread “main” java.lang.NullPointerException
at java.util.HashMap.merge(HashMap.java:1216)
at java.util.stream.Collectors.lambda$toMap$150(Collectors.java:1320)
.....
复制代码
起因是 toMap() 办法中应用 Map.merge() 办法合并时,merge 不容许 value 为 null 导致的,源码如下:
复制代码
default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
// 在这里判断了 value 不可为 null
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value);
...
复制代码
解决办法
业务管制不要呈现 Null 值【有 Null 的中央,能够赋值默认值】
在转换时加判断,如果为 null,则给一个默认值
Map<String, String> map = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, sdsTest -> sdsTest.getAge() == null ? “0” : sdsTest.getAge()));
应用 collect(..) 构建,容许空值
Map<String, String> nmap = sdsTests.stream().collect(HashMap::new,(k, v) -> k.put(v.getName(), v.getAge()), HashMap::putAll);
// TODO 上游业务从 Map 取值要做 NPE 判断
应用 Optional 对值进行包装
复制代码
Map<String, Optional<String>> opmap = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, sdsTest -> Optional.ofNullable(sdsTest.getAge())));
System.out.println(“bbb.age=” + opmap.get(“bbb”).orElse(“0”));
输入:
bbb.age=0
复制代码
倡议
优先业务管制,尽量避免 List 中存在 Null
其次举荐第 4 种办法【应用 Optional 对值进行包装】,能很好的防止 NPE 问题
key 反复危险
java.lang.IllegalStateException: Duplicate key xx
当 List 中有反复值的时候,应用 Collectors.toMap() 转为 Map 时,会报:java.lang.IllegalStateException: Duplicate key xx,例如
复制代码
List<SdsTest> sdsTests = new ArrayList<>();
SdsTest sds1 = new SdsTest("aaa","aaa");
SdsTest sds2 = new SdsTest("aaa","ccc");
sdsTests.add(sds1);
sdsTests.add(sds2);
Map<String, String> map = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, SdsTest::getAge));
System.out.println(map.toString());
运行谬误:
Exception in thread “main” java.lang.IllegalStateException: Duplicate key aaa
at java.util.stream.Collectors.lambda$throwingMerger$92(Collectors.java:133)
at java.util.stream.Collectors$$Lambda$6/1177096266.apply(Unknown Source)
at java.util.HashMap.merge(HashMap.java:1245)
.....
复制代码
起因是两个参数的 toMap(xx, xx)办法, 当呈现反复 key 触发 merge 时,间接抛出异样。源码如下:
复制代码
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper) {// 留神这里的 throwingMerger()
return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new);
}
复制代码
接下来咱们看 throwingMerger() 办法:【留神办法正文】
复制代码
/**
- Returns a merge function, suitable for use in
- {@link Map#merge(Object, Object, BiFunction) Map.merge()} or
- {@link #toMap(Function, Function, BinaryOperator) toMap()}, which always
- throws {@code IllegalStateException}. This can be used to enforce the
- assumption that the elements being collected are distinct.
* - @param <T> the type of input arguments to the merge function
- @return a merge function which always throw {@code IllegalStateException}
*/
private static <T> BinaryOperator<T> throwingMerger() {
return (u,v) -> {throw new IllegalStateException(String.format(“Duplicate key %s”, u)); };
}
…
复制代码
解决办法
业务管制尽量不要呈现反复值
呈现反复 key 时,应用前面的 value 笼罩后面的 value
复制代码
SdsTest sds1 = new SdsTest(“aaa”,”aaa”);
SdsTest sds2 = new SdsTest(“bbb”,”bbb”);
SdsTest sds3 = new SdsTest(“aaa”,”ccc”);
sdsTests.add(sds1);
sdsTests.add(sds2);
sdsTests.add(sds3);
// 写法一
Map<String, String> nmap = sdsTests.stream().collect(HashMap::new,(k, v) -> k.put(v.getName(), v.getAge()), HashMap::putAll);
System.out.println(“nmap->:” + nmap.toString());
// 写法二
Map<String, String> nmap1 = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, SdsTest::getAge, (k1, k2) -> k2));
System.out.println(“nmap1->:” + nmap1.toString());
…
输入:
nmap->:{aaa=ccc, bbb=bbb}
nmap1->:{aaa=ccc, bbb=bbb}
复制代码
呈现反复 key 时,把对应的 value 拼接起来
复制代码
…
Map<String, String> nmap1 = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName, SdsTest::getAge, (k1, k2) -> k1 + “,” + k2));
System.out.println(“nmap1->:” + nmap1.toString());
…
输入:
nmap1->:{aaa=aaa,ccc, bbb=bbb}
复制代码
把反复 key 的值拼成一个汇合
复制代码
……
Map<String, List<String>> map = sdsTests.stream().collect(Collectors.toMap(SdsTest::getName,
s -> {List<String> ages = new ArrayList<>();
ages.add(s.getAge());
return ages;
},
(List<String> v1, List<String> v2) -> {v1.addAll(v2);
return v1;
}));
System.out.println(“map->”+map.toString());
输入:
map->{aaa=[aaa, ccc], bbb=[bbb]}