java 将列表合并为具有唯一元素的单个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5057156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:12:54  来源:igfitidea点击:

Merging lists into a single array with unique elements

java

提问by Rajat Gupta

I have five lists List<String>. I want to get a single array which contains all elements from those lists, eliminating any repartitions. What could be the best way to do this?

我有五个清单List<String>。我想得到一个包含这些列表中所有元素的单个数组,消除任何重新分区。什么是最好的方法来做到这一点?

`Edit: Can someone also comment on the performance of HashSet in comparision to List. I am concerned about performance, as I am doing this job while calculating the data to be displayed on a webpage. And no. of elements in the set would be high around 300-400, what parameters whould be suitable for the Set?

`编辑:有人还可以评论 HashSet 与 List 相比的性能吗?我担心性能,因为我在计算要显示在网页上的数据时正在做这项工作。和不。集合中的元素数量会高在 300-400 左右,哪些参数适合该集合?

My elements in set, would be of this type: <HColumn<String, String>>

我在集合中的元素将是这种类型: <HColumn<String, String>>

回答by Johan Sj?berg

Set<String> set = new HashSet<String>();
set.addAll(list1);
...
set.addAll(list5);
String[] str = set.toArray(new String[0]);

回答by Brian Agnew

You can add the list contents into a Set.

您可以将列表内容添加到Set 中

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

不包含重复元素的集合。更正式地说,集合不包含一对元素 e1 和 e2,使得 e1.equals(e2),并且最多包含一个空元素。正如其名称所暗示的那样,该接口对数学集合抽象进行建模。

You can then get an array back by calling Set.toArray().

然后您可以通过调用返回一个数组Set.toArray()

A standard set won't preserve order, but duplicates will be eliminated.

标准集不会保留顺序,但会消除重复项。

回答by Sergey Vedernikov

Create HashSetand call addAll with your Lists

创建HashSet并使用您的列表调用 addAll

回答by prashant khunte

Same question was asked to me in interview, but combine two vectors without repeating common object and without using collection framework methods.

面试时问了我同样的问题,但结合两个向量而不重复公共对象,也不使用集合框架方法。

package com.pra.sss;
/*
 * Vector v1 and V2 contain some common objects
 * Create vector V3 which will contain all V1 and V2 
 * and common objects only once 
 * without using colletion framework functions 
 */import java.util.Vector;

public class VectorTest {
public static void main(String[] args) {
   Vector<String> v1 = new Vector<String>();
   Vector<String> v2 = new Vector<String>();
   Vector<String> v3 = new Vector<String>();
   /*Vector v1*/
   v1.add("A");
   v1.add("B");
   v1.add("C");
   v1.add("D");
   /*Vector v2*/
   v2.add("W");
   v2.add("B");
   v2.add("C");
   v2.add("Z");

   System.out.println("V1"+v1);
   System.out.println("V2"+v2);

   for(String ss : v1) {
       v3.add(ss);
   }

   for (int i = 0; i < v1.size(); i++) {
       for (int j = i; j < v2.size(); j++) {
           if(v1.get(i).equals(v2.get(j))) {
               break;
           }
           else{
               v3.add(v2.get(j));
               break;
           }
    }
  }
   System.out.println("v3:"+v3);
}
}

/*
output
V1[A, B, C, D]
V2[W, B, C, Z]
v3:[A, B, C, D, W, Z]
*/

回答by lukastymo

The best way to merge lists depends if you want have the same order of elements or not. i.d. elements 1st list before elements 2nd.

合并列表的最佳方法取决于您是否希望元素的顺序相同。id 元素第 1 个列表在第 2 个元素之前。

In @Johan Sj?berg solution it is used HashSet. The elements are returned in no particular order. If you want save the same order better use collection with predictable iteration order e.g. LinkedHashSet.

在@Johan Sj?berg 解决方案中,它使用了 HashSet。元素没有特定的顺序返回。如果您想保存相同的顺序,最好使用具有可预测迭代顺序的集合,例如 LinkedHashSet。