Program to convert ArrayList to LinkedList in Java


Let’s say the following is our ArrayList −

List<String> arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");

Now, convert this ArrayList to LinkedList using toCollection() −

List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));

Example

Following is the program to convert ArrayList to LinkedList in Java −

import java.util.*;
import java.util.stream.*;
public class Demo {
   public static void main(String args[]) {
      List<String> arrList = Arrays.asList("John", "Jacob", "Kevin", "Katie", "Nathan");
      System.out.println("ArrayList = " + arrList);
      List<String> myList = arrList.stream().collect(Collectors.toCollection(LinkedList::new));
      System.out.println("LinkedList (ArrayList to LinkedList) = " + myList);
   }
}

Output

ArrayList = [John, Jacob, Kevin, Katie, Nathan]
LinkedList (ArrayList to LinkedList) = [John, Jacob, Kevin, Katie, Nathan]

Updated on: 25-Sep-2019

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements