英文:
How to efficiently merge two lists in Java?
问题
有几种方法可以合并Java中的列表
- 您可以调用
ArrayList(Collection<? extends E> c)
- 您可以使用流API,如
Stream.concat()
或Stream.of(listA, listB).forEach()
- 还有更多方法...
哪种方法对于将两个随机访问列表合并成一个新的随机访问列表来说最节省内存和性能最高?
英文:
There are several ways to merge lists in Java
- You can call to
ArrayList(Collection<? extends E> c)
- You can use the stream API, like
Stream.concat()
orStream.of(listA, listB).forEach()
- and more...
What would be the most memory and performance efficient way to merge two random access lists into a new random access list?
答案1
得分: 5
Sure, here's the translation:
你可以使用Apache commons库-
ListUtils.union(listA, listB);
对于大型数据集,使用并行的Java 8 Streams 可能比仅使用Streams更好。
Stream.concat(list1.parallelStream(), list1.parallelStream())
.collect(Collectors.toList());
英文:
you can use Apache commons library-
ListUtils.union(listA, listB);
Using parallel Java8 Streams could be is better instead of just streams for large datasets.
Stream.concat(list1.parallelStream(), list1.parallelStream())
.collect(Collectors.toList());
答案2
得分: 4
你还没有在你的上下文中定义“合并”是什么意思。这个答案假设它的意思是“合并成一个列表”。
为了减少内存和处理的使用量,创建一个大小恰好合适的列表,然后依次将每个列表添加到其中。
List<E> result = new ArrayList<>(list1.size() + list2.size());
result.addAll(list1);
result.addAll(list2);
这样可以消除在list1.addAll(list2)
期间可能发生的可能多余的内存分配和对象创建。
英文:
You have not defined what "merge" means in your context. This answer assumes it means "combine into one list".
To reduce the amount of memory and processing used, create a List whose size is exactly right, then add each list in turn to it.
List<E> result = new ArrayList<>(list1.size() + list2.size());
result.addAll(list1);
result.addAll(list2);
This eliminates possible redundant memory allocation and object creation that may occur during list1.addAll(list2)
.
答案3
得分: 1
以下是您要翻译的代码部分:
尝试使用此方法创建包含所有元素的不可变列表,通过执行浅复制。请注意,对源列表的更改将反映在结果列表中(因此实际上的不可变性取决于输入列表的不可变性/访问)。
public class MergedList<T> extends AbstractList<T> {
private final List<T>[] lists;
private final int size;
@SafeVarargs
MergedList(List<T>... lists) {
this.lists = lists.clone();
this.size = Arrays.stream(lists).mapToInt(list -> list.size()).sum();
}
@Override
public T get(int index) {
for (List<T> list : lists)
if (index < list.size())
return list.get(index);
else
index -= list.size();
throw new IndexOutOfBoundsException("index");
}
@Override
public int size() {
return size;
}
}
以及
List<Integer> a = List.of(1, 2, 3, 4);
List<Integer> b = List.of(5, 6, 7);
List<Integer> c = new MergedList<>(a, b);
System.out.println(c);
输出
[1, 2, 3, 4, 5, 6, 7]
考虑到原始列表已更新,最好删除字段 `size` 并执行以下操作:
@Override
public int size() {
return Arrays.stream(lists).mapToInt(list -> list.size()).sum();
}
英文:
Try this to create an immutable list containing all the elements, by performing a shallow copy. Beware that changes to the source lists will be reflected in the resulting list (so the immutability in reality depends on the immutability / access to the input lists).
public class MergedList<T> extends AbstractList<T> {
private final List<T>[] lists;
private final int size;
@SafeVarargs
MergedList(List<T>... lists) {
this.lists = lists.clone();
this.size = Arrays.stream(lists).mapToInt(list -> list.size()).sum();
}
@Override
public T get(int index) {
for (List<T> list : lists)
if (index < list.size())
return list.get(index);
else
index -= list.size();
throw new IndexOutOfBoundsException("index");
}
@Override
public int size() {
return size;
}
}
and
List<Integer> a = List.of(1, 2, 3, 4);
List<Integer> b = List.of(5, 6, 7);
List<Integer> c = new MergedList<>(a, b);
System.out.println(c);
output
[1, 2, 3, 4, 5, 6, 7]
Considering that the original list is updated, it might be better to remove the field size
and do this:
@Override
public int size() {
return Arrays.stream(lists).mapToInt(list -> list.size()).sum();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论