mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-06 13:48:00 +08:00
!822 CollUtil.unionAll优化:初始化一次size,防止ArrayList多次扩容
Merge pull request !822 from 这是一个腊鸡/v5-dev
This commit is contained in:
commit
d30e8ab55b
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user