问题描述:
java.lang.NullPointerException: element cannot be mapped to a null key
问题分析:
1、使用Collectors.groupingBy()进行分组时,分组值存在null值。
List<String> strList = new ArrayList<>(Arrays.asList("11", "12", "13", null, null));
Map<String, List<String>> map = strList.stream().collect(Collectors.groupingBy(x -> x));
解决办法:分组值为null时,默认值为空字符。
List<String> strList = new ArrayList<>(Arrays.asList("11", "12", "13", null, null));
Map<String, List<String>> map = strList.stream().collect(Collectors.groupingBy(x -> StrUtil.isEmpty(x) ? "" : x));