Java Program to append all elements of other Collection to ArrayList

Java 8Object Oriented ProgrammingProgramming

Let us first create an ArrayList −

ArrayList<String>arr = new ArrayList<String>();
arr.add("50");
arr.add("100");
arr.add("150");
arr.add("200");
arr.add("250");
arr.add("300");

Now, let’s say we have another Collection as Vector −

Vector<String>vector = new Vector<String>();
vector.add("500");
vector.add("700");
vector.add("800");
vector.add("1000");
Append all the elements from the Vector to ArrayList:
arr.addAll(vector);

Example

 Live Demo

import java.util.ArrayList;
import java.util.Vector;
public class Demo {
   public static void main(String[] args) {
      ArrayList<String>arr = new ArrayList<String>();
      arr.add("50");
      arr.add("100");
      arr.add("150");
      arr.add("200");
      arr.add("250");
      arr.add("300");
      Vector<String>vector = new Vector<String>();
      vector.add("500");
      vector.add("700");
      vector.add("800");
      vector.add("1000");
      // append
      arr.addAll(vector);
      System.out.println("Result after append...");
      for (String str: arr)
      System.out.println(str);
   }
}

Output

Result after append...
50
100
150
200
250
300
500
700
800
1000
raja
Published on 23-Apr-2019 08:57:17
Advertisements