背景
针对于集合转换成map的时候出现value为空造成的空指针情况,进行解决
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student student = new Student();
student.setName("AA");
list.add(student);
student = new Student();
student.setName("BB");
list.add(student);
student = new Student();
student.setName("BB");
student.setPetName("B1");
list.add(student);
student = new Student();
student.setName("CC");
student.setPetName("C1");
list.add(student);
student = new Student();
student.setName("CC");
list.add(student);
Map<String, String> collect = list.stream().collect(Collectors.toMap(x -> x.getName(), x -> x.getPetName(), (a, b) -> a));
System.out.println(collect);
}
@Data
static
class Student {
private String name;
private String petName;
}
}
在根据学生实体的名称作为key和昵称作为value的时候,当petName为空的时候会出现空指针
Exception in thread "main" java.lang.NullPointerException
at java.util.HashMap.merge(HashMap.java:1224)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.aimsphm.console.symptom.service.impl.T.main(T.java:44)
解决方式
解决方式1
对空判断
Map<String, String> collect1 = list.stream().collect(Collectors.toMap(x -> x.getName(),
x -> Objects.nonNull(x.getPetName()) ? x.getPetName() : "",
(a, b) -> StringUtils.isNotBlank(a) ? a : b));
System.out.println(collect1);
解决方式2
使用Optional对空值进行处理
Map<String, String> collect2 = list.stream().collect(Collectors.toMap(
x -> Optional.ofNullable(x).map(Student::getName).orElse(""),
x -> Optional.ofNullable(x).map(Student::getPetName).orElse(""),
(a, b) -> StringUtils.isNotBlank(a) ? a : b));
System.out.println(collect2);
解决方式3
使用 Collectors.reducing()重新对分组之后的数据进行计算
Map<String, String> collect3 = list.stream().collect(Collectors.groupingBy(x ->
x.getName(), HashMap::new, Collectors.reducing(null,
x -> x.getPetName(), (a, b) -> Objects.nonNull(a) ? a : b)));
System.out.println(collect3);
解决方式4
直接在collect方法中进行厘定
Map<String, String> collect4 = list.stream().collect(HashMap::new, (map, item) -> {
if (!map.containsKey(item.getName()) || Objects.isNull(map.get(item.getName()))) {
map.put(item.getName(), item.getPetName());
}
}, HashMap::putAll);
System.out.println(collect4);
最终的结果输出为
## 输出的结果中有的使用了空字符串,可能需要根据实际需要进行调整
## 因为map是无序的,所以结果顺序并不一致
{CC=C1, BB=B1, AA=}
{CC=C1, BB=B1, AA=}
{CC=C1, BB=B1, AA=null}
{AA=null, BB=B1, CC=C1}
其中name没有考虑为空的情况, 可以使用filter进行过滤