Convert Array to List in Java

Tags: Array ArrayList Convert

In this tutorial, we are going to show how to convert an array in Java into a List object. We show different example programs that use core Java APIs to convert arrays in different Java versions.

Using Arrays.asList() method

In this Java example program, we use the method Arrays.asList() to create an ArrayList object from an array.

import java.util.Arrays;
import java.util.List;

public class ArrayToListExample1 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = Arrays.asList(inputArray);

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.Arrays$ArrayList

Using for loop to add items to ArrayList

The following Java example to show you how to use for loop in Java to loop every item of an array and add each item into an ArrayList object.

import java.util.ArrayList;
import java.util.List;

public class ArrayToListExample2 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = new ArrayList<>();
        for(String input: inputArray) {
            list.add(input);
        }

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.ArrayList

Using Java 8 Stream

Following examples, we use Java 8 Stream API to convert an array into List.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListExample3 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = Arrays.stream(inputArray).collect(Collectors.toList());

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.ArrayList

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListExample4 {
    public static void main(String[] args) {
        int[] inputArray = {1, 2, 3, 4, 5};

        List<Integer> list = Arrays.stream(inputArray).boxed().collect(Collectors.toList());

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[1, 2, 3, 4, 5]
class java.util.ArrayList

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListExample5 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = Arrays.stream(inputArray).collect(Collectors.toCollection(ArrayList::new));

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.ArrayList

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListExample6 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = Arrays.stream(inputArray).collect(Collectors.toCollection(LinkedList::new));

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.LinkedList

Using Java 9 List.of() method

Java 9 provides method List.of() to create an unmodifiable list from an array.

import java.util.List;

public class ArrayToListExample7 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = List.of(inputArray);

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.ImmutableCollections$ListN

Using Java 10 Collectors.toUnmodifiableList() method

Java 10 provides Collectors.toUnmodifiableList() method to create an unmodifiable List.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayToListExample8 {
    public static void main(String[] args) {
        String[] inputArray = {"Simple", "Solution", "Java", "Array"};

        List<String> list = Arrays.stream(inputArray).collect(Collectors.toUnmodifiableList());

        System.out.println(list);
        System.out.println(list.getClass());
    }
}
The output is:
[Simple, Solution, Java, Array]
class java.util.ImmutableCollections$ListN

Happy Coding 😊

Java Convert ArrayList to Array

Java Convert ArrayList to String

Java Convert ArrayList to Comma Separated String

Java Convert ArrayList to HashSet

Java Convert ArrayList to LinkedList

How to use Java ArrayList with Examples

Java Create New Array with Class Type and Length

Java Create ArrayList using List Interface

Java Get Index of Minimum Value in ArrayList