From 0b8c4bbad02b280a22115c8b383a288b77eda887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E9=9F=B5?= <1320162752@qq.com> Date: Mon, 26 Sep 2022 21:33:49 +0800 Subject: [PATCH] =?UTF-8?q?CollUtil.unionAll=E4=BC=98=E5=8C=96=EF=BC=9A?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=80=E6=AC=A1size=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2ArrayList=E5=A4=9A=E6=AC=A1=E6=89=A9=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/collection/CollUtil.java | 42 ++++++++++------ .../hutool/core/collection/CollUtilTest.java | 48 +++++++++++++++++++ 2 files changed, 75 insertions(+), 15 deletions(-) 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 977dbcd87..ba1b99b23 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 @@ -207,27 +207,39 @@ public class CollUtil { */ @SafeVarargs public static List unionAll(Collection coll1, Collection coll2, Collection... otherColls) { - final List result; - if (isEmpty(coll1)) { - result = new ArrayList<>(); - } else { - result = new ArrayList<>(coll1); + if (CollUtil.isEmpty(coll1) && CollUtil.isEmpty(coll2) && ArrayUtil.isEmpty(otherColls)) { + return Collections.emptyList(); } - if (isNotEmpty(coll2)) { - result.addAll(coll2); - } - - if (ArrayUtil.isNotEmpty(otherColls)) { + // 计算元素总数 + int totalSize = 0; + totalSize += size(coll1); + totalSize += size(coll2); + if (otherColls != null) { for (Collection otherColl : otherColls) { - if (isEmpty(otherColl)) { - continue; - } - result.addAll(otherColl); + totalSize += size(otherColl); } } - return result; + // 根据size创建,防止多次扩容 + List res = new ArrayList<>(totalSize); + if (coll1 != null) { + res.addAll(coll1); + } + if (coll2 != null) { + res.addAll(coll2); + } + if (otherColls == null) { + return res; + } + + for (Collection otherColl : otherColls) { + if (otherColl != null) { + res.addAll(otherColl); + } + } + + return res; } /** 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 68883a538..6ff5c4339 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 @@ -940,6 +940,54 @@ public class CollUtilTest { final List list3 = null; final List list = CollUtil.unionAll(list1, list2, list3); Assert.assertNotNull(list); + + final List resList2 = CollUtil.unionAll(null, null, null); + Assert.assertNotNull(resList2); + } + + @Test + public void unionAllOrdinaryTest() { + final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); + final List list2 = CollectionUtil.newArrayList(1, 2, 3); + final List list3 = CollectionUtil.newArrayList(4, 5, 6); + 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()); + } + + @Test + public void unionAllTwoOrdinaryTest() { + final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); + final List list2 = CollectionUtil.newArrayList(1, 2, 3); + 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()); + } + + @Test + public void unionAllOtherIsNullTest() { + final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); + final List list2 = CollectionUtil.newArrayList(1, 2, 3); + 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()); + } + + @Test + public void unionAllOtherTwoNullTest() { + final List list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3); + final List list2 = CollectionUtil.newArrayList(1, 2, 3); + 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()); } @Test