Program to convert Array to List in Java

Java 8Object Oriented ProgrammingProgramming

The array is as follows −

String arr[] = { "One", "Two", "Three", "Four", "Five" };

Now, using the Array.asList() to convert the above array to list −

List<String>myList = Arrays.asList(arr);

Example

Following is the program to convert Array to List in Java −

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      String arr[] = { "One", "Two", "Three", "Four", "Five" };
      System.out.println("Array = "+ Arrays.toString(arr));
      List<String>myList = Arrays.asList(arr);
      System.out.println("List (Array to List) = " + myList);
   }
}

Output

Array = [One, Two, Three, Four, Five]
List (Array to List) = [One, Two, Three, Four, Five]
raja
Published on 25-Sep-2019 07:45:21
Advertisements