Program to convert ArrayList to LinkedList in Java

Java 8Object Oriented ProgrammingProgramming

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]
raja
Published on 25-Sep-2019 07:50:22
Advertisements