3

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 ();

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

 C = [1,2,3,4,5,6]; 
4
  • 1
    A.addAll(B) will add all B elements to A Commented Dec 24, 2012 at 9:57
  • Use a Set implementation for merge without duplication Commented Dec 24, 2012 at 10:01
  • If you got the answer then accept that and if you are still not found desire answer then please paste your problem including exception if it is. Commented Dec 24, 2012 at 10:26
  • How to reach this result: C = [1,2] - only unique items? Commented Feb 17, 2019 at 22:25

8 Answers 8

4

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

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 
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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.

2 Comments

this is better but only if original lists can be modified.
If you want the first list not to be modified then you can store it in a temporary list.A very common coding scheme:)
0

Declaration of A and B seems to be wrong.

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

Comments

0
 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); 

Comments

0

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

Based on your request, you should merge both lists without repeating the items using a 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); 

Comments

0
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)); 

1 Comment

TreeSet maintains the data sorted, not ordered. LinkedHashSet maintains the data ordered but not sorted.
0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.