4

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.

MariuszS
  • 27,972
  • 12
  • 103
  • 144
Suniel
  • 1,369
  • 7
  • 22
  • 39
  • possible duplicate of [Java ArrayList: Merging ArrayLists within ArrayLists to create one ArrayList](http://stackoverflow.com/questions/3975783/java-arraylist-merging-arraylists-within-arraylists-to-create-one-arraylist) – Lijo Jan 09 '14 at 08:47

9 Answers9

7

You combine a foreach loop and the addAll method.

Example

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

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

How this works?

A for each loop will traverse through every member of a Collection. It has a temporary variable, in this case list that it assigns the current element too. All you're doing is adding every element inside each value for list, to one ArrayList named combined.

christopher
  • 24,892
  • 3
  • 50
  • 86
4

This should work

ArrayList<ArrayList<String>> a = new ArrayList<ArrayList<String>>();
        List<String> result = new ArrayList<String>();
        for (ArrayList<String> arrayList : a) {
            result.addAll(arrayList);
        }

Look into main loop and get each list in it and add to your result list.

Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
4

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

Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
  • 1
    Note that this way you can end up with many references "duplicated". If you have large lists (number of contained elements), consider looking here: https://stackoverflow.com/a/13868352/11152683 – Lubo Apr 07 '20 at 05:12
4

We have some other ways too, If you can use Apache commons-collection

ListUtils.union(java.util.List list1, java.util.List list2)

Returns a new list containing the second list appended to the first list.

gowtham
  • 949
  • 7
  • 15
2

Use ArrayList.addAll(). Something like this should work (assuming lists contain String objects; you should change accordingly).

List<String> combined = new ArrayList<String>();
combined.addAll(firstArrayList);
Lijo
  • 4,738
  • 1
  • 41
  • 55
1

If you need an Iterable, you can use Guava:

Iterables.concat(Iterable<? extends Iterable<? extends T>> inputs)

And if you really need a List, you can cast a resulting Iterable to a List using this:

Lists.newArrayList(Iterable<? extends E> elements)

or

Lists.newLinkedList(Iterable<? extends E> elements)
Alexander Tokarev
  • 2,643
  • 2
  • 16
  • 21
0

Java 8 streams provide another solution:

List<List<String>> list = Arrays.asList(
        Arrays.asList("1", "2"),
        Arrays.asList("3", "4"),
        Arrays.asList("5", "6")
);

List<String> merged = list
        .stream()
        .reduce(new ArrayList<>(),(accumulator, sublist) -> {accumulator.addAll(sublist);return accumulator;});

System.out.println(merged);

It is similar to the accepted answer: you loop through your list (using Stream.reduce) to add all of your sublists elements to your merged list.

Abrikot
  • 836
  • 1
  • 7
  • 18
0
    List<Integer> one = Arrays.asList(1, 2,3);
    List<Integer> two = Arrays.asList(4, 5,6);
    List<Integer> out = Stream.of(one, two)
            .collect(ArrayList::new, (listStream, item) -> listStream.addAll(item), (item1, item2) -> {});
    System.out.println(out);
Vinayak Dornala
  • 1,339
  • 1
  • 15
  • 22
-1

Merging lists without loop with Guava

Using FluentIterable.transformAndConcat.

Applies function to each element of this fluent iterable and returns a fluent iterable with the concatenated combination of results. function returns an Iterable of results.

Usage

List<String> combined = FluentIterable.from(a)
      .transformAndConcat(Functions.identity())
      .toList();
MariuszS
  • 27,972
  • 12
  • 103
  • 144