Skip to content

Convert Array to LinkedList in Java Example

This example shows how to convert an array to the LinkedList in Java. The example also shows how to convert array to a linked list using asList and addAll methods.

How to convert an array to LinkedList in Java?

There are a couple of ways using which we can convert an array to a LinkedList object in Java.

1. Using the asList method

The LinkedList class does not provide any direct method to convert an array to the linked list. So we will first convert an array to the list and then create a linked list object from that list.

The asList method of the Arrays class converts an array to the list.

The List object returned by this method is backed by the original array. Once we get the List from the array, we will create a linked list by using this array as given below.

Output

2. Using the addAll method

In this approach, we will first create an empty LinkedList object and then add all the elements of an array to the LinkedList object using the addAll method of the Collections class.

Output

This approach is a preferred way to convert an array to a linked list object since it does not involve an additional step of converting to a list from an array.

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

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

References:
Java 8 LinkedList

About the author

Leave a Reply

Your email address will not be published.