diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java index 63d5faa56..7c95075af 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollStreamUtilTest.java @@ -203,6 +203,20 @@ public class CollStreamUtilTest { groupThen); // 总之,如果你是想要group分组后还要进行别的操作,用它就对了! + // 并且对null值进行了友好处理,例如 + List students = Arrays.asList(null, null, new Student(1, 1, 1, "张三"), + new Student(1, 2, 1, "李四")); + Map> termIdStudentsMap = CollStreamUtil.groupBy(students, Student::getTermId, Collectors.toList()); + Map> termIdStudentsCompareMap = new HashMap<>(); + termIdStudentsCompareMap.put(null, Arrays.asList(null, null)); + termIdStudentsCompareMap.put(1L, Arrays.asList(new Student(1L, 1, 1, "张三"), new Student(1L, 2, 1, "李四"))); + Assert.assertEquals(termIdStudentsCompareMap, termIdStudentsMap); + + Map termIdCountMap = CollStreamUtil.groupBy(students, Student::getTermId, Collectors.counting()); + Map termIdCountCompareMap = new HashMap<>(); + termIdCountCompareMap.put(null, 2L); + termIdCountCompareMap.put(1L, 2L); + Assert.assertEquals(termIdCountCompareMap, termIdCountMap); }