!822 CollUtil.unionAll优化:初始化一次size,防止ArrayList多次扩容

Merge pull request !822 from 这是一个腊鸡/v5-dev
This commit is contained in:
Looly 2022-09-26 14:08:41 +00:00 committed by Gitee
commit d30e8ab55b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 75 additions and 15 deletions

View File

@ -207,27 +207,39 @@ public class CollUtil {
*/ */
@SafeVarargs @SafeVarargs
public static <T> List<T> unionAll(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) { public static <T> List<T> unionAll(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) {
final List<T> result; if (CollUtil.isEmpty(coll1) && CollUtil.isEmpty(coll2) && ArrayUtil.isEmpty(otherColls)) {
if (isEmpty(coll1)) { return Collections.emptyList();
result = new ArrayList<>();
} else {
result = new ArrayList<>(coll1);
} }
if (isNotEmpty(coll2)) { // 计算元素总数
result.addAll(coll2); int totalSize = 0;
} totalSize += size(coll1);
totalSize += size(coll2);
if (ArrayUtil.isNotEmpty(otherColls)) { if (otherColls != null) {
for (Collection<T> otherColl : otherColls) { for (Collection<T> otherColl : otherColls) {
if (isEmpty(otherColl)) { totalSize += size(otherColl);
continue;
}
result.addAll(otherColl);
} }
} }
return result; // 根据size创建防止多次扩容
List<T> res = new ArrayList<>(totalSize);
if (coll1 != null) {
res.addAll(coll1);
}
if (coll2 != null) {
res.addAll(coll2);
}
if (otherColls == null) {
return res;
}
for (Collection<T> otherColl : otherColls) {
if (otherColl != null) {
res.addAll(otherColl);
}
}
return res;
} }
/** /**

View File

@ -940,6 +940,54 @@ public class CollUtilTest {
final List<String> list3 = null; final List<String> list3 = null;
final List<String> list = CollUtil.unionAll(list1, list2, list3); final List<String> list = CollUtil.unionAll(list1, list2, list3);
Assert.assertNotNull(list); Assert.assertNotNull(list);
final List<String> resList2 = CollUtil.unionAll(null, null, null);
Assert.assertNotNull(resList2);
}
@Test
public void unionAllOrdinaryTest() {
final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
final List<Integer> list3 = CollectionUtil.newArrayList(4, 5, 6);
final List<Integer> 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<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
final List<Integer> 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<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
final List<Integer> 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<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
final List<Integer> 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 @Test