How to Convert LinkedList to ArrayList in java

In this tutorial, we are converting a LinkedList to ArrayList in Java. I have already shared what is the difference between LinkedList and ArrayList in Java. We are storing the name of the fruits in the LinkedList of Strings. After converting LinkedList to ArrayList, we are showing the elements of ArrayList to ensure that ArrayList is having the same elements that we have in LinkedList.

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.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry