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:

  1. 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:

    1. Get the Array to be converted.
    2. Create an empty List
    3. Iterate through the items in the Array.
    4. For each item, add it to the List
    5. Return the formed List

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to List
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an Array to List
        public static <T> List<T> convertArrayToList(T array[])
        {
      
            // Create an empty List
            List<T> list = new ArrayList<>();
      
            // Iterate through the array
            for (T t : array) {
                // Add each element into the list
                list.add(t);
            }
      
            // Return the converted List
            return list;
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks",
                                        "A Computer Portal" };
      
            // Print the Array
            System.out.println("Array: " 
                           + Arrays.toString(array));
      
            // convert the Array to List
            List<String>
                list = convertArrayToList(array);
      
            // Print the List
            System.out.println("List: " + list);
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    List: [Geeks, forGeeks, A computer Portal]
    
  2. 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:

    1. Get the Array to be converted.
    2. Create the List by passing the Array as parameter in the constructor of the List with the help of Arrays.asList() method
    3. Return the formed List

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to List
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an Array to List
        public static <T> List<T> convertArrayToList(T array[])
        {
      
            // Create the List by passing the Array
            // as parameter in the constructor
            List<T> list = Arrays.asList(array);
      
            // Return the converted List
            return list;
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks",
                                        "A computer Portal" };
      
            // Print the Array
            System.out.println("Array: " 
                               + Arrays.toString(array));
      
            // convert the Array to List
            List<String>
                list = convertArrayToList(array);
      
            // Print the List
            System.out.println("List: " + list);
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    List: [Geeks, forGeeks, A computer Portal]
    
  3. 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:


    1. Get the Array to be converted.
    2. Create an empty List.
    3. Add the array into the List by passing it as the parameter to the Collections.addAll() method.
    4. Return the formed List

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to List
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an Array to List
        public static <T> List<T> convertArrayToList(T array[])
        {
      
            // Create the List by passing the Array
            // as parameter in the constructor
            List<T> list = new ArrayList<>();
      
            // Add the array to list
            Collections.addAll(list, array);
      
            // Return the converted List
            return list;
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A computer Portal" };
      
            // Print the Array
            System.out.println("Array: " 
                                  + Arrays.toString(array));
      
            // convert the Array to List
            List<String>
                list = convertArrayToList(array);
      
            // Print the List
            System.out.println("List: " + list);
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    List: [Geeks, forGeeks, A computer Portal]
    
  4. 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:

    1. Get the Array to be converted.
    2. Convert the array to Stream
    3. Convert the Stream to List using Collectors.toList()
    4. Collect the formed list using collect() method
    5. Return the formed List

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to List in Java 8
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert array to list
        public static <T> List<T> convertArrayToList(T array[])
        {
            // create a list from the Array
            return Arrays
                .stream(array)
                .collect(Collectors.toList());
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A computer Portal" };
      
            // Print the Array
            System.out.println("Array: "
                                + Arrays.toString(array));
      
            // convert the Array to List
            List<String>
                list = convertArrayToList(array);
      
            // Print the List
            System.out.println("List: " + list);
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    List: [Geeks, forGeeks, A computer Portal]
    
  5. Using Guava Lists.newArrayList(): Lists.newArrayList() creates a mutable ArrayList instance containing the elements of the specified array.

    Algorithm:

    1. Get the Array to be converted.
    2. Create an empty List.
    3. Add the array into the List by passing it as the parameter to the Lists.newArrayList() method.
    4. Return the formed List
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // Array to List in Java 8
      
    import static com.google.common.collect.Lists.*;
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert array to list
        public static <T> List<T> convertArrayToList(T array[])
        {
            // create a list from the Array
            return Lists.newArrayList(array);
        }
      
        public static void main(String args[])
        {
            // Create an Array
            String array[] = { "Geeks", "forGeeks"
                                        "A computer Portal" };
      
            // Print the Array
            System.out.println("Array: " 
                                 + Arrays.toString(array));
      
            // convert the Array to List
            List<String>
                list = convertArrayToList(array);
      
            // Print the List
            System.out.println("List: " + list);
        }
    }

    chevron_right

    
    

    Output:

    Array: [Geeks, forGeeks, A computer Portal]
    List: [Geeks, forGeeks, A computer Portal]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.