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

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “How to convert LinkedList to ArrayList in Java”

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.