mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
CollUtil.unionAll优化:初始化一次size,防止ArrayList多次扩容
This commit is contained in:
parent
ec5965f49c
commit
0b8c4bbad0
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user