Java Array To LinkedList

Java Array To LinkedList | The array is a container that stores elements of similar data types in a continuous way i.e. array is a contiguous allocation of memory. Once the array is created it stores the elements in a continuous index. The LinkedList is a container that has a node containing data and information. The data stores the element and the information contain the address of the next node, unlike the array which is not like continuous memory allocation.

Array To LinkedList in Java using asList() Method

The List interface contains the asList() method to convert the array to LinkedList. The syntax of the asList() method is:- public static List asList(T… a)

Here we have created the array of strings containing several emotions and then read this array as a List and then convert it to a LinkedList.

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {

   public static void main(String[] args) {
      String[] emotions = { "Happy", "Sad", "Angry", "Depressed" };

      // convert array to List
      List<String> list = Arrays.asList(emotions);

      // convert list to LinkedList
      LinkedList<String> linkedList = new LinkedList<String>(list);

      System.out.println("Array: " + Arrays.asList(emotions));
      System.out.println("Type: " + emotions.getClass().getName());
      System.out.println("LinkedList: " + linkedList);
      System.out.println("Type: " + linkedList.getClass().getName());
   }
}

Output:-

Array: [Happy, Sad, Angry, Depressed]
Type: [Ljava.lang.String;
LinkedList: [Happy, Sad, Angry, Depressed]
Type: java.util.LinkedList

The asList() method returns a fixed-size list backed by the specified array. Changes made to the array will be visible in the returned list, and changes made to the list will be visible in the array. But since we are creating LinkedList from the List therefore modifications done in array/List will not affect the LinkedList object. Below program demonstrate it:-

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
      String[] emotions = { "Happy", "Sad", "Angry", "Depressed" };
      List<String> list = Arrays.asList(emotions);
      LinkedList<String> linkedList = new LinkedList<String>(list);

      emotions[2] = "Anxiety";
      System.out.println("Array: " + Arrays.toString(emotions));
      System.out.println("List: " + list);
      System.out.println("LinkedList: " + linkedList);
   }
}

Output:-

Array: [Happy, Sad, Anxiety, Depressed]
List: [Happy, Sad, Anxiety, Depressed]
LinkedList: [Happy, Sad, Angry, Depressed]

Convert Array To LinkedList in Java Using Collections.addAll()

Here we are using the addAll() method in the collections framework to add all the elements to the LinkedList. The method syntax of addAll() method is:- public static boolean addAll(Collection c, T… elements)

import java.util.Collections;
import java.util.LinkedList;

public class Main {
   public static void main(String[] args) {
      String[] emotions = { "Harry", "Henry", "Joseph", "John" };
      LinkedList<String> linkedList = new LinkedList<String>();
      Collections.addAll(linkedList, emotions);

      System.out.println("LinkedList: " + linkedList);
      System.out.println("Type: " + linkedList.getClass().getName());
   }
}

Output:-

LinkedList: [Harry, Henry, Joseph, John]
Type: java.util.LinkedList

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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