How to convert LinkedList to ArrayList in Java

Learn to convert LinkedList to ArrayList in Java with example. We will also learn to convert arraylist to linkedlist in Java.

1. Convert LinkedList to ArrayList

To convert a linkedlist containing objects to an arraylist containing similar objects, we can use the arraylist constructor which accepts another collection and initialize the arraylist with the elements of linkedlist.

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

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        LinkedList<String> linkedList = new LinkedList<>();
        
        linkedList.add("A");
        linkedList.add("B");
        linkedList.add("C");
        linkedList.add("D");
        
       ArrayList<String> arrayList = new ArrayList<>(linkedList);
       
       System.out.println(arrayList);
    }
}

Program output.

[A, B, C, D]

2. Convert ArrayList to LinkedList

The conversion from arraylist to linkedlist is very similar to previous example. Here we have to use the linkedlist constructor. It accepts another collection and initialize the linkedlist with the elements of arraylist.

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

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        ArrayList<String> arrayList = new ArrayList<>();
        
        arrayList.add("A");
        arrayList.add("B");
        arrayList.add("C");
        arrayList.add("D");
        
       LinkedList<String> linkedlist = new LinkedList<>(arrayList);
       
       System.out.println(linkedlist);
    }
}

Program output.

[A, B, C, D]

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs
LinkedList Java Docs

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.