Collections

How to add arraylist to another arraylist in java

public boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)

package com.candidjava.arrayList;

import java.util.ArrayList;

public class ArrayListAddArray 
{
  public static void main(String[] args) {
    
    ArrayList<String> al1=new ArrayList<String>();

    al1.add("candid");    
    al1.add("mathan");
    al1.add("antony");
  
    System.out.println("First arraylist ");
    System.out.println(al1);
    
    
    ArrayList<String> al2=new ArrayList<String>();

    al2.add("chennai");    
    al2.add("tiruchy");
    al2.add("madurai");
  
    System.out.println("Second arraylist ");
    System.out.println(al2);
    
    // adding arraylist 1 to arraylist 2 using addAll
    al2.addAll(al1);
    System.out.println("After adding arraylist 1 to arrayList 2");
    System.out.println(al2);  
  }
}

OUTPUT

First arraylist 
[candid, mathan, antony]
Second arraylist 
[chennai, tiruchy, madurai]
After adding arraylist 1 to arrayList 2
[chennai, tiruchy, madurai, candid, mathan, antony]