From d343051db52d53186a3449b675f5335fe3f46b02 Mon Sep 17 00:00:00 2001 From: wulang Date: Wed, 29 Mar 2023 16:30:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:CollUtil.split=E4=BC=98=E5=8C=96=E5=88=87?= =?UTF-8?q?=E5=89=B2=E5=88=97=E8=A1=A8=E5=8F=82=E6=95=B0=E5=88=A4=E6=96=AD?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8DOOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- .../cn/hutool/core/collection/CollUtil.java | 9 ++- .../hutool/core/collection/CollUtilTest.java | 80 ++++++++++--------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f54b17698..2a1f91ca1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ * 【core 】 SerializeUtil.deserialize增加白名单类,避免RCE vulnerability(issue#3021@Github) ### 🐞Bug修复 - +* 【core 】 CollUtil.split优化切割列表参数判断,避免OOM ------------------------------------------------------------------------------------------------------------- # 5.8.16 (2023-03-26) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 9f46e9254..e0a10b7e4 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -371,7 +371,7 @@ public class CollUtil { result.addAll(coll1); } result.removeAll(coll2); - } catch (UnsupportedOperationException e){ + } catch (UnsupportedOperationException e) { // 针对 coll1 为只读集合的补偿 result = CollUtil.create(AbstractCollection.class); result.addAll(coll1); @@ -998,7 +998,7 @@ public class CollUtil { * * @param 集合元素类型 * @param collectionType 集合类型,rawtype 如 ArrayList.class, EnumSet.class ... - * @param elementType 集合元素类型 + * @param elementType 集合元素类型 * @return 集合类型对应的实例 * @since v5 */ @@ -1189,11 +1189,12 @@ public class CollUtil { return result; } - ArrayList subList = new ArrayList<>(size); + int initSize = Math.min(collection.size(), size); + List subList = new ArrayList<>(initSize); for (T t : collection) { if (subList.size() >= size) { result.add(subList); - subList = new ArrayList<>(size); + subList = new ArrayList<>(initSize); } subList.add(t); } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 75ab23e26..4ddee73ef 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -255,6 +255,14 @@ public class CollUtilTest { Assert.assertEquals(3, split.get(0).size()); } + @Test + public void splitTest2() { + final ArrayList list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); + final List> split = CollUtil.split(list, Integer.MAX_VALUE); + Assert.assertEquals(3, split.size()); + Assert.assertEquals(3, split.get(0).size()); + } + @Test public void foreachTest() { final HashMap map = MapUtil.newHashMap(); @@ -381,9 +389,9 @@ public class CollUtilTest { @Test public void sortByPropertyTest() { final List list = CollUtil.newArrayList( - new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// + new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// ); CollUtil.sortByProperty(list, "createTime"); @@ -395,9 +403,9 @@ public class CollUtilTest { @Test public void sortByPropertyTest2() { final List list = CollUtil.newArrayList( - new TestBean("张三", 0, DateUtil.parse("2018-05-01")), // - new TestBean("李四", -12, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 23, DateUtil.parse("2018-04-01"))// + new TestBean("张三", 0, DateUtil.parse("2018-05-01")), // + new TestBean("李四", -12, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 23, DateUtil.parse("2018-04-01"))// ); CollUtil.sortByProperty(list, "age"); @@ -409,8 +417,8 @@ public class CollUtilTest { @Test public void fieldValueMapTest() { final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 12, DateUtil.parse("2018-04-01"))// ); final Map map = CollUtil.fieldValueMap(list, "name"); @@ -422,8 +430,8 @@ public class CollUtilTest { @Test public void fieldValueAsMapTest() { final List list = CollUtil.newArrayList(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), // - new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // - new TestBean("王五", 14, DateUtil.parse("2018-04-01"))// + new TestBean("李四", 13, DateUtil.parse("2018-03-01")), // + new TestBean("王五", 14, DateUtil.parse("2018-04-01"))// ); final Map map = CollUtil.fieldValueAsMap(list, "name", "age"); @@ -756,14 +764,14 @@ public class CollUtilTest { Assert.assertFalse(CollUtil.addIfAbsent(null, "123")); Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList("123"), "123")); Assert.assertFalse(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Animal("jack", 20))); + new Animal("jack", 20))); // 正常情况 Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList("456"), "123")); Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Dog("jack", 20))); + new Dog("jack", 20))); Assert.assertTrue(CollUtil.addIfAbsent(CollUtil.newArrayList(new Animal("jack", 20)), - new Animal("tom", 20))); + new Animal("tom", 20))); } @Test @@ -774,9 +782,9 @@ public class CollUtilTest { oldMap.put("c", "134"); final Map map = CollUtil.toMap(oldMap.entrySet(), - new HashMap<>(), - Map.Entry::getKey, - entry -> Long.parseLong(entry.getValue())); + new HashMap<>(), + Map.Entry::getKey, + entry -> Long.parseLong(entry.getValue())); Assert.assertEquals(1L, (long) map.get("a")); Assert.assertEquals(12L, (long) map.get("b")); @@ -848,12 +856,12 @@ public class CollUtilTest { public void setValueByMapTest() { // https://gitee.com/dromara/hutool/pulls/482 final List people = Arrays.asList( - new Person("aa", 12, "man", 1), - new Person("bb", 13, "woman", 2), - new Person("cc", 14, "man", 3), - new Person("dd", 15, "woman", 4), - new Person("ee", 16, "woman", 5), - new Person("ff", 17, "man", 6) + new Person("aa", 12, "man", 1), + new Person("bb", 13, "woman", 2), + new Person("cc", 14, "man", 3), + new Person("dd", 15, "woman", 4), + new Person("ee", 16, "woman", 5), + new Person("ff", 17, "man", 6) ); final Map genderMap = new HashMap<>(); @@ -894,12 +902,12 @@ public class CollUtilTest { @Test public void distinctByFunctionTest() { final List people = Arrays.asList( - new Person("aa", 12, "man", 1), - new Person("bb", 13, "woman", 2), - new Person("cc", 14, "man", 3), - new Person("dd", 15, "woman", 4), - new Person("ee", 16, "woman", 5), - new Person("ff", 17, "man", 6) + new Person("aa", 12, "man", 1), + new Person("bb", 13, "woman", 2), + new Person("cc", 14, "man", 3), + new Person("dd", 15, "woman", 4), + new Person("ee", 16, "woman", 5), + new Person("ff", 17, "man", 6) ); // 覆盖模式下ff覆盖了aa,ee覆盖了bb @@ -953,8 +961,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3, 4, 5, 6).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3, 4, 5, 6).toArray(), + list.toArray()); } @Test @@ -964,8 +972,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @Test @@ -975,8 +983,8 @@ public class CollUtilTest { @SuppressWarnings("ConfusingArgumentToVarargsMethod") final List list = CollUtil.unionAll(list1, list2, null); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @Test @@ -986,8 +994,8 @@ public class CollUtilTest { final List list = CollUtil.unionAll(list1, list2, null, null); Assert.assertNotNull(list); Assert.assertArrayEquals( - CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), - list.toArray()); + CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(), + list.toArray()); } @Test