Program to convert Array to List in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
Examples:
Input: Array: [Geeks, forGeeks, A computer Portal]
Output: List: [Geeks, forGeeks, A computer Portal]Input: Array: [1, 2, 3, 4, 5]
Output: List: [1, 2, 3, 4, 5]
Below are methods to convert Array to List in Java:
- Brute Force or Naive Method: In this method, an empty List is created and all elements present of the Array are added to it one by one.
Algorithm:
- Get the Array to be converted.
- Create an empty List
- Iterate through the items in the Array.
- For each item, add it to the List
- Return the formed List
Program:
// Java Program to convert// Array to Listimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Listpublicstatic<T> List<T> convertArrayToList(T array[]){// Create an empty ListList<T> list =newArrayList<>();// Iterate through the arrayfor(T t : array) {// Add each element into the listlist.add(t);}// Return the converted Listreturnlist;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A Computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to ListList<String>list = convertArrayToList(array);// Print the ListSystem.out.println("List: "+ list);}}chevron_rightfilter_noneOutput:Array: [Geeks, forGeeks, A computer Portal] List: [Geeks, forGeeks, A computer Portal]
- Using Arrays.asList() method: In this method, the Array is passed as the parameter into the List constructor with the help of Arrays.asList() method.
Algorithm:
- Get the Array to be converted.
- Create the List by passing the Array as parameter in the constructor of the List with the help of Arrays.asList() method
- Return the formed List
Program:
// Java Program to convert// Array to Listimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Listpublicstatic<T> List<T> convertArrayToList(T array[]){// Create the List by passing the Array// as parameter in the constructorList<T> list = Arrays.asList(array);// Return the converted Listreturnlist;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to ListList<String>list = convertArrayToList(array);// Print the ListSystem.out.println("List: "+ list);}}chevron_rightfilter_noneOutput:Array: [Geeks, forGeeks, A computer Portal] List: [Geeks, forGeeks, A computer Portal]
- Using Collections.addAll(): Since List is a part of the Collection package in Java. Therefore the Array can be converted into the List with the help of Collections.addAll() method.
Algorithm:
- Get the Array to be converted.
- Create an empty List.
- Add the array into the List by passing it as the parameter to the Collections.addAll() method.
- Return the formed List
Program:
// Java Program to convert// Array to Listimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an Array to Listpublicstatic<T> List<T> convertArrayToList(T array[]){// Create the List by passing the Array// as parameter in the constructorList<T> list =newArrayList<>();// Add the array to listCollections.addAll(list, array);// Return the converted Listreturnlist;}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to ListList<String>list = convertArrayToList(array);// Print the ListSystem.out.println("List: "+ list);}}chevron_rightfilter_noneOutput:Array: [Geeks, forGeeks, A computer Portal] List: [Geeks, forGeeks, A computer Portal]
- Using Java 8 Stream API: ArrayList constructor can take another collection object to construct a new list containing the elements of the specied array.
Algorithm:
- Get the Array to be converted.
- Convert the array to Stream
- Convert the Stream to List using Collectors.toList()
- Collect the formed list using collect() method
- Return the formed List
Program:
// Java Program to convert// Array to List in Java 8importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert array to listpublicstatic<T> List<T> convertArrayToList(T array[]){// create a list from the ArrayreturnArrays.stream(array).collect(Collectors.toList());}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to ListList<String>list = convertArrayToList(array);// Print the ListSystem.out.println("List: "+ list);}}chevron_rightfilter_noneOutput:
Array: [Geeks, forGeeks, A computer Portal] List: [Geeks, forGeeks, A computer Portal]
- Using Guava Lists.newArrayList(): Lists.newArrayList() creates a mutable ArrayList instance containing the elements of the specified array.
Algorithm:
- Get the Array to be converted.
- Create an empty List.
- Add the array into the List by passing it as the parameter to the Lists.newArrayList() method.
- Return the formed List
// Java Program to convert// Array to List in Java 8importstaticcom.google.common.collect.Lists.*;importjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert array to listpublicstatic<T> List<T> convertArrayToList(T array[]){// create a list from the ArrayreturnLists.newArrayList(array);}publicstaticvoidmain(String args[]){// Create an ArrayString array[] = {"Geeks","forGeeks","A computer Portal"};// Print the ArraySystem.out.println("Array: "+ Arrays.toString(array));// convert the Array to ListList<String>list = convertArrayToList(array);// Print the ListSystem.out.println("List: "+ list);}}chevron_rightfilter_noneOutput:
Array: [Geeks, forGeeks, A computer Portal] List: [Geeks, forGeeks, A computer Portal]
Recommended Posts:
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Program to Convert Set to List in Java
- Program to Convert List to Map in Java
- Program to Convert List to Stream in Java
- Program to Convert a Vector to List in Java
- Program to convert Array to Set in Java
- Program to Convert Stream to an Array in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Primitive Array to Stream in Java
- Program to convert Byte Array to Writer in Java
- Java Program to convert Character Array to IntStream
- Program to Convert Set of Integer to Array of Integer in Java
- Convert List to Set in Java
- Convert an Iterator to a List in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

