Merge two list into a single list

collections, java, list, merge

Solution

Just iterate through all the inner lists of `a` using `foreach` loop and `addAll` to `result` arraylist

ArrayList<String> merged = new ArrayList<String>();

for(ArrayList<String> list : a){
   merged.addAll(list);
}

EDIT: As @Lubo pointed out.

Note that this way you can end up with many arrays being created and thrown away internally in `ArrayList`. If you have large lists (number of contained elements), consider looking here: Union List

Problem

I have a ArrayList as below. ``` ArrayList<ArrayList<String>> a = new ArrayList<ArrayList<String>>(); ``` Where ArrayList `'a'` contains two ArrayList of string as below. ``` [a,b,c,d] & [1,2,3,4] ``` How to merge these two list into a single list as below. ``` [a,b,c,d,1,2,3,4] ``` Thanks In Advance.

Original source

Related problems