Java Collection How to - Append all elements of other Collection to ArrayList








Question

We would like to know how to append all elements of other Collection to ArrayList.

Answer

import java.util.ArrayList;
import java.util.Vector;
/*from   w ww . j a va 2  s. c  om*/
public class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");

    Vector<String> v = new Vector<String>();
    v.add("4");
    v.add("5");

    // append all elements of Vector to ArrayList
    arrayList.addAll(v);

    for (String str : arrayList)
      System.out.println(str);

  }

}

The code above generates the following result.