java 如何将两个列表字符串合并为一个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14019751/
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-31 14:46:47  来源:igfitidea点击:

How to combine two list string into one list

javalistcollections

提问by Samosir

I have two list string different,

我有两个不同的列表字符串,

List<String> A= [1,2,3,4]; 
List<String> B= [1,2,5,6]; 

And I want to combine two list, in new list string in List C = new Arraylist ();

我想合并两个列表,在新的列表字符串中 List C = new Arraylist();

how to combine two list string , be like the example:

如何组合两个列表字符串,例如:

    C = [1,2,3,4,5,6];

回答by Aleksander Blomsk?ld

Use Collection.addAll(), and a TreeSet, to remove duplicates and keep the result sorted.

使用Collection.addAll()TreeSet删除重复项并保持结果排序。

Set<String> c = new TreeSet<String>(a); //create a Set with all the elements in a
c.addAll(b); //add all the elements in b

回答by ma?ek

This will get them combined

这将使它们结合起来

combined = new ArrayList<String>();
combined.addAll(A);
combined.addAll(B);

This will get the uniques

这将获得唯一性

List<String> uniques = new ArrayList<String>(new HashSet<String>(combined));

回答by Abhishek_Mishra

Do it like this :

像这样做 :

listOne.removeAll(listTwo);
listTwo.addAll(listOne);
Collections.sort(listTwo);

You can remove the third line if you do not want it to be sorted.

如果您不想对其进行排序,可以删除第三行。

回答by Luiggi Mendoza

There are two ways to merge the results of both lists: using List#addAllor Set#addAll. The main difference between both is heavily explained here: What is the difference between Set and List?

有两种方法可以合并两个列表的结果:使用List#addAllSet#addAll。两者之间的主要区别在这里进行了大量解释:Set 和 List 之间有什么区别?

Based on your request, you should merge both lists without repeating the items using a Set

根据您的要求,您应该合并两个列表,而不要使用 Set

List<String> lstA = new ArrayList<String>();
lstA.add("1");
lstA.add("2");
lstA.add("3");
lstA.add("4");
List<String> lstB = new ArrayList<String>();
lstA.add("1");
lstA.add("2");
lstA.add("5");
lstA.add("6");
Set<String> lstC = new LinkedHashSet<String>();
lstC.addAll(A);
lstC.addAll(B);
List<String> lstAB = new ArrayList(lstC);

回答by An Cong Tran

Declaration of A and B seems to be wrong.

A 和 B 的声明似乎是错误的。

However, given two lists A and B, to combine them into C, you can use the following code: C.addAll(A); C.addAll(B);

但是,给定两个列表 A 和 B,要将它们组合成 C,可以使用以下代码: C.addAll(A); C.addAll(B);

回答by Sandeep Pathak

List<String> A= new ArrayList<String>();
List<String> B= new ArrayList<String>();

Set<String> set = new TreeSet<String>(A);
set.addAll(B);
System.out.println(new ArrayList<String>(set));

回答by Rahul

  Set<String> set = new TreeSet<String>(A); // for keeping the output sorted else you can also use java.util.HashSet
  set.addAll(B);
  List<String> finalList = new ArrayList<String>(set);

回答by Kiran Mohan

To get a List<String>in sorted order, use this piece of code

要获得List<String>排序顺序,请使用这段代码

    String a[] = {"1","2","3","4"};
    String b[] = {"1","2","5","6"};
    List<String> A= Arrays.asList(a); 
    List<String> B= Arrays.asList(b); 

    Set<String> CTemp = new TreeSet<>();  
    CTemp.addAll(A);
    CTemp.addAll(B);

    List<String> C = new ArrayList<>(CTemp);