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