Array is contiguous memory allocation while LinkedList is a block of elements randomly placed in the memory which are linked together where a block is holding the address of another block in memory. Sometimes as per requirement or because of space issues in memory where there are bigger chunks of code in the enterprising world it becomes necessary to convert arrays to List. Here conversion of array to LinkedList is demonstrated.
Methods:
Method 1: Using asList() method of Collections class
This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serialized and implements RandomAccess. This runs in O(1) time.
Syntax:
public static List asList(T... a) ;
Parameters: This method takes the array a which is required to be converted into a List. Here array of parameters works similar to an object array parameter.
Approach:
- Create an array.
- Convert the array to List.
- Create LinkedList from the List using the constructor.
Example
Java
// Java Program to convert Array to LinkedList // Importing array, List & LinkedList classes from // java.util package import java.util.Arrays; import java.util.LinkedList; import java.util.List; // Class public class GFG { // Main driver method public static void main(String[] args) { // Create a string Array String[] strArr = { "A" , "B" , "C" , "D" , "E" }; // Convert array to list List<String> list = Arrays.asList(strArr); // Create a LinkedList and // pass List in LinkedList constructor LinkedList<String> linkedList = new LinkedList<String>(list); // Display and print all elements of LinkedList System.out.println( "LinkedList of above array : " + linkedList); } } |
LinkedList of above array : [A, B, C, D, E]
Method 2: Using addAll() method of Collections class
This method is used to append all the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator.
Syntax:
boolean addAll(Collection C) ;
Parameters: The parameter C is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the list.
Return Value: The method returns true if at least one action of append is performed else return false.
Approach:
- Create an array.
- Create an empty LinkedList.
- Use addAll() method of collections class which takes two objects as parameters.
- First object as where to be converted
- Second object as which to be converted.
Example
Java
// Java Program to convert Array to LinkedList // Importing array, List & LinkedList, Collections classes // from java.util package import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; // Class public class GFG { // Main driver method public static void main(String[] args) { // Create an Array // here string type String[] strArr = { "G" , "E" , "E" , "K" , "S" }; // Create an empty LinkedList LinkedList<String> linkedList = new LinkedList<String>(); // Append all elements of array to linked list // using Collections.addAll() method Collections.addAll(linkedList, strArr); // Print the above LinkedList received System.out.println( "Converted LinkedList : " +linkedList); } } |
Converted LinkedList : [G, E, E, K, S]