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

This commit is contained in:
青韵 2022-09-26 21:33:49 +08:00
parent ec5965f49c
commit 0b8c4bbad0
2 changed files with 75 additions and 15 deletions

View File

@ -207,27 +207,39 @@ public class CollUtil {
*/
@SafeVarargs
public static <T> List<T> unionAll(Collection<T> coll1, Collection<T> coll2, Collection<T>... otherColls) {
final List<T> 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<T> otherColl : otherColls) {
if (isEmpty(otherColl)) {
continue;
}
result.addAll(otherColl);
totalSize += size(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> list = CollUtil.unionAll(list1, list2, list3);
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