The addAll() method is used to add all elements of a list to arraylist in java.
Syntax:
arrayList.addAll(list);
Example:
package com.w3schools; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); List<String> list = new ArrayList<String>(); list.add("Vikas"); list.add("Lalit"); arrayList.addAll(list); System.out.println("After adding list elements: "+arrayList); } } |
Output
Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal] After adding list elements: [Jai, Mahesh, Vivek, Naren, Hemant, Vishal, Vikas, Lalit] |