mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
处理了CollStreamUtil.toMap中,如果value为空时,发生NPE异常问题
This commit is contained in:
parent
6cb86ce509
commit
a2331bf5c9
@ -81,7 +81,7 @@ public class CollStreamUtil {
|
|||||||
if (CollUtil.isEmpty(collection)) {
|
if (CollUtil.isEmpty(collection)) {
|
||||||
return Collections.emptyMap();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,13 +6,7 @@ import lombok.ToString;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CollectionStream测试方法
|
* CollectionStream测试方法
|
||||||
@ -51,6 +45,11 @@ public class CollStreamUtilTest {
|
|||||||
Assert.assertEquals(map.get(2L), "李四");
|
Assert.assertEquals(map.get(2L), "李四");
|
||||||
Assert.assertEquals(map.get(3L), "王五");
|
Assert.assertEquals(map.get(3L), "王五");
|
||||||
Assert.assertNull(map.get(4L));
|
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
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user