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