From 3e330f1c647281f2518ef155c9cfb3909a6e82e9 Mon Sep 17 00:00:00 2001 From: qibinhang Date: Fri, 8 Aug 2025 13:18:32 +0800 Subject: [PATCH] Add tests for covering additional, important scenarios of `CollStreamUtil`. --- .../core/collection/CollStreamUtilTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) 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 1bfb2fb66..2699c32a7 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 @@ -60,6 +60,40 @@ public class CollStreamUtilTest { assertNull(map.get(4L)); } + @Test + public void testToMap_KeyCollision_SilentlyOverwrite() { + List list = new ArrayList<>(); + list.add(new Student(1, 101, 1, "张三")); + list.add(new Student(1, 102, 1, "李四")); + Map map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName, false); + + assertEquals(1, map.size()); + assertEquals("李四", map.get(1L)); // 确保后面的值覆盖前面的 + } + + @Test + public void testToMap_NullKeyOrValue() { + List list = new ArrayList<>(); + list.add(new Student(1, 1, 1L, "张三")); + list.add(null); + list.add(new Student(1, 2, 2L, null)); + + assertThrows(NullPointerException.class, () -> { + CollStreamUtil.toMap(list, Student::getStudentId, Student::getName); + }); + } + + @Test + public void testToMap_LargeInputPerformance() { + List list = new ArrayList<>(); + for (long i = 0; i < 10000; i++) { + list.add(new Student(1, 1, i, "学生" + i)); + } + Map map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName); + + assertEquals(10000, map.size()); + } + @Test public void testGroupByKey() { Map> map = CollStreamUtil.groupByKey(null, Student::getClassId);