处理了CollStreamUtil.toMap中,如果value为空时,发生NPE异常问题

This commit is contained in:
VampireAchao 2021-12-06 21:01:14 +08:00 committed by achao
parent 6cb86ce509
commit a2331bf5c9
2 changed files with 7 additions and 8 deletions

View File

@ -81,7 +81,7 @@ public class CollStreamUtil {
if (CollUtil.isEmpty(collection)) {
return Collections.emptyMap();
}
return StreamUtil.of(collection, isParallel).collect(Collectors.toMap(key, value, (l, r) -> l));
return StreamUtil.of(collection, isParallel).collect(HashMap::new, (HashMap<K, V> m, E v) -> m.put(key.apply(v), value.apply(v)), HashMap::putAll);
}

View File

@ -6,13 +6,7 @@ import lombok.ToString;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* CollectionStream测试方法
@ -51,6 +45,11 @@ public class CollStreamUtilTest {
Assert.assertEquals(map.get(2L), "李四");
Assert.assertEquals(map.get(3L), "王五");
Assert.assertNull(map.get(4L));
// 测试value为空时
list.add(new Student(1, 1, 4, null));
map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
Assert.assertNull(map.get(4L));
}
@Test