如何高效地在Java中合并两个列表?

huangapple go评论108阅读模式
英文:

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&lt;? extends E&gt; c)
  • You can use the stream API, like Stream.concat() or Stream.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库-

  1. ListUtils.union(listA, listB);

对于大型数据集,使用并行的Java 8 Streams 可能比仅使用Streams更好。

  1. Stream.concat(list1.parallelStream(), list1.parallelStream())
  2. .collect(Collectors.toList());
英文:

you can use Apache commons library-

  1. ListUtils.union(listA, listB);

Using parallel Java8 Streams could be is better instead of just streams for large datasets.

  1. Stream.concat(list1.parallelStream(), list1.parallelStream())
  2. .collect(Collectors.toList());

答案2

得分: 4

你还没有在你的上下文中定义“合并”是什么意思。这个答案假设它的意思是“合并成一个列表”。

为了减少内存和处理的使用量,创建一个大小恰好合适的列表,然后依次将每个列表添加到其中。

  1. List<E> result = new ArrayList<>(list1.size() + list2.size());
  2. result.addAll(list1);
  3. 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.

  1. List&lt;E&gt; result = new ArrayList&lt;&gt;(list1.size() + list2.size());
  2. result.addAll(list1);
  3. result.addAll(list2);

This eliminates possible redundant memory allocation and object creation that may occur during list1.addAll(list2).

答案3

得分: 1

以下是您要翻译的代码部分:

尝试使用此方法创建包含所有元素的不可变列表,通过执行浅复制。请注意,对源列表的更改将反映在结果列表中(因此实际上的不可变性取决于输入列表的不可变性/访问)。

  1. public class MergedList&lt;T&gt; extends AbstractList&lt;T&gt; {
  2. private final List&lt;T&gt;[] lists;
  3. private final int size;
  4. @SafeVarargs
  5. MergedList(List&lt;T&gt;... lists) {
  6. this.lists = lists.clone();
  7. this.size = Arrays.stream(lists).mapToInt(list -&gt; list.size()).sum();
  8. }
  9. @Override
  10. public T get(int index) {
  11. for (List&lt;T&gt; list : lists)
  12. if (index &lt; list.size())
  13. return list.get(index);
  14. else
  15. index -= list.size();
  16. throw new IndexOutOfBoundsException(&quot;index&quot;);
  17. }
  18. @Override
  19. public int size() {
  20. return size;
  21. }
  22. }

以及

  1. List&lt;Integer&gt; a = List.of(1, 2, 3, 4);
  2. List&lt;Integer&gt; b = List.of(5, 6, 7);
  3. List&lt;Integer&gt; c = new MergedList&lt;&gt;(a, b);
  4. System.out.println(c);
  5. 输出
  6. [1, 2, 3, 4, 5, 6, 7]
  7. 考虑到原始列表已更新最好删除字段 `size` 并执行以下操作
  8. @Override
  9. public int size() {
  10. return Arrays.stream(lists).mapToInt(list -&gt; list.size()).sum();
  11. }
英文:

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).

  1. public class MergedList&lt;T&gt; extends AbstractList&lt;T&gt; {
  2. private final List&lt;T&gt;[] lists;
  3. private final int size;
  4. @SafeVarargs
  5. MergedList(List&lt;T&gt;... lists) {
  6. this.lists = lists.clone();
  7. this.size = Arrays.stream(lists).mapToInt(list -&gt; list.size()).sum();
  8. }
  9. @Override
  10. public T get(int index) {
  11. for (List&lt;T&gt; list : lists)
  12. if (index &lt; list.size())
  13. return list.get(index);
  14. else
  15. index -= list.size();
  16. throw new IndexOutOfBoundsException(&quot;index&quot;);
  17. }
  18. @Override
  19. public int size() {
  20. return size;
  21. }
  22. }

and

  1. List&lt;Integer&gt; a = List.of(1, 2, 3, 4);
  2. List&lt;Integer&gt; b = List.of(5, 6, 7);
  3. List&lt;Integer&gt; c = new MergedList&lt;&gt;(a, b);
  4. System.out.println(c);

output

  1. [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:

  1. @Override
  2. public int size() {
  3. return Arrays.stream(lists).mapToInt(list -&gt; list.size()).sum();
  4. }

huangapple
  • 本文由 发表于 2020年8月11日 07:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63349403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定