Read Also: Check if LinkedList is Empty in Java
According to Oracle docs, we are using ArrayList constructor to convert LinkedList to ArrayList. The syntax is given below:
new ArrayList(Collection c)
where we pass LinkedList to the Collection c and it will convert to the ArrayList.
Convert LinkedList to ArrayList Example
In the below Java program, the LinkedList object is created. Then the LinkedList.add() method is used to add elements to the LinkedList object. After that, an ArrayList list is created and it is initialized using the elements of the LinkedList using a parameterized constructor.import java.util.*; class LinkedListToArrayList { public static void main(String args[]) { // Creating LinkedList Object LinkedList<String> linkedlist = new LinkedList<String>(); linkedlist.add("Mango"); linkedlist.add("Banana");
linkedlist.add("Pear"); linkedlist.add("Apple"); linkedlist.add("Orange"); // Converting LinkedList to ArrayList List<String> list = new ArrayList(linkedlist); for (String s : list) { System.out.println(s); } } }
Output:
Mango
Banana
Pear
Apple
Orange
That's all for today. Please mention in the comments in case you have any questions related to how to convert LinkedList to ArrayList in Java.