Append All Elements Of List To LinkedList In Java
Chapter:
Data Structures
Last Updated:
05-09-2016 16:24:01 UTC
Program:
/* ............... START ............... */
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class JavaAppendAllElementOfListToLinkedList {
public static void main(String[] args) {
// create a LinkedList
LinkedList<String> list = new LinkedList<String>();
// Add elements to the LinkedList
list.add("Java");
list.add("Pascal");
list.add("Cobol");
list.add("C");
// Displaying linked list before add
System.out.println("Before: LinkedList: " + list);
// create a new list having few elements
List<String> arrayList = new ArrayList<String>();
arrayList.add("Item1");
arrayList.add("Item2");
arrayList.add("Item3");
// Append the list elements to LinkedList
list.addAll(arrayList);
// Displaying the LinkedList after addAll
System.out.println("After: LinkedList: " + list);
}
}
/* ............... END ............... */
Output
Before: LinkedList: [Java, Pascal, Cobol, C]
After: LinkedList: [Java, Pascal, Cobol, C, Item1, Item2, Item3]
Notes:
-
public boolean addAll(Collection<? extends E> 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.
Tags
Append All Elements Of List To LinkedList, Java, Data Structures