Convert LinkedList to Array in Java Example

This example shows how to convert LinkedList to an array in Java. This example also shows how to convert a LinkedList of Wrapper objects to an equivalent primitive array using the toArray method.

How to convert LinkedList to an array in Java?

There are several ways using which you can convert a linked list object to an array in Java.

1. By iterating the LinkedList

The simplest way to convert to an array is to iterate the LinkedList and add linked list elements to an array one by one as given below.


2. Using the toArray method

You can use the toArray method of the LinkedList class to convert to an array from the LinkedList object.

The toArray method returns an array containing all the elements of the linked list in the proper sequence.

Important Note:

If the specified array is large enough to hold all the elements of the LinkedList, the same array is returned with the list elements. If not, a new array is created, filled with the elements and returned. If the specified array is larger than the linked list, the array element that immediately comes after the list element is set to null to mark the boundary of the list elements.

As a good practice, always pass an array with the same size as that of the LinkedList object to avoid the creation of a new array.


3. Convert LinkedList of Wrapper objects to an array of primitive values using Apache Commons

If you are using the Apache Commons library, then the ArrayUtils class provides the toPrimitive method which can be used to convert LinkedList of wrapper objects to an equivalent array of primitive values along with the toArray method (for example, LinkedList of Double objects to an array of double).

The toPrimitive method is overloaded for all primitive types.


This example is a part of the LinkedList in Java tutorial.

Please let me know your views in the comments section below.

Java 8 LinkedList

About the author



My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping