How to add all elements of a list to LinkedList

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AddAllElement {

public static void main(String[] args) {

LinkedList<String> linkedList = new LinkedList<String>();

linkedList.add(“Mumbai”);
linkedList.add(“Delhi”);
linkedList.add(“Noida”);
linkedList.add(“Gao”);
linkedList.add(“Patna”);

//print linkedlist
System.out.println(“Linklist is = “+linkedList);

List<String> brr = new LinkedList<String>();
brr.add(“one”);
brr.add(“second”);

//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.

linkedList.addAll(brr);

System.out.println(“After AddAll list brr =”+linkedList);

List<String> arr = new ArrayList<String>();
arr.add(“Delhi”);
arr.add(“Kurukshetra”);

linkedList.addAll(arr);

System.out.println(“After AddAll list arraylist”+linkedList);

}
}

Output:

Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

After AddAll list brr =[Mumbai, Delhi, Noida, Gao, Patna, one, second]

After AddAll list arraylist[Mumbai, Delhi, Noida, Gao, Patna, one, second, Delhi, Kurukshetra]

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 31 = 39