Convert an Array to a List in Java
This post will discuss how to convert an array to a list using plain Java, Guava, and Apache Commons Collections.
There are two general-purpose list implementations in Java — ArrayList and LinkedList. This post will use ArrayList, which offers constant-time positional access and is just plain fast.
1. Naive solution
A naive solution is to create an empty list and push every element of the specified array into the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.List; import java.util.ArrayList; class Main { // Generic method to convert an array to an `ArrayList` public static <T> List<T> convertToList(T[] arr) { // create an empty list List<T> list = new ArrayList<>(); // push each array element into the list for (T i: arr) { list.add(i); } // return the list return list; } // Program to convert an array to a list in Java public static void main(String[] args) { // given string array String[] str = { "A", "B", "C", "D" }; // convert array to a list List<String> list = convertToList(str); // print list System.out.println(list); } } |
Output:
[A, B, C, D]
2. Using Arrays.asList() method
Arrays.asList() returns a fixed-size list backed by the specified array. Since an array cannot be structurally modified, the list will throw an UnsupportedOperationException if we try to add or remove elements from it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Program to convert an array to a list in Java public static void main(String[] args) { // given string array String[] str = { "A", "B", "C", "D" }; // convert array to a list using `Arrays.asList()` method List<String> list = Arrays.asList(str); // print list System.out.println(list); } } |
Output:
[A, B, C, D]
If we need a list that can expand or shrink, we can use:
|
1 |
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D")); |
3. Using Collections.addAll() method
We can use Collections.addAll() to add all elements of the specified array to the specified list, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to convert an array to an `ArrayList` public static <T> List<T> convertToList(T[] arr) { // create an empty list List<T> list = new ArrayList<>(); // Adds all elements of the specified array to the specified list Collections.addAll(list, arr); // return the list return list; } |
We can also use CollectionUtils.addAll() provided by Apache Commons Collections that works in a similar way as Collections.addAll().
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Generic method to convert an array to an `ArrayList` public static <T> List<T> convertToList(T[] arr) { // create an empty list List<T> list = new ArrayList<>(); // Adds all the array elements to the given list CollectionUtils.addAll(list, arr); // return the list return list; } |
4. Using Java 8
We can use Java 8 Stream to convert an array to a list. We first convert the specified array to a sequential Stream and then use a collector to accumulate the input elements into a new List.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { // Generic method to convert an array to a list public static <T> List<T> convertToList(T[] arr) { return Arrays.stream(arr).collect(Collectors.toList()); // or // return Stream.of(arr).collect(Collectors.toList()); } // Program to convert an array to a list in Java public static void main(String[] args) { // given string array String[] arr = { "A", "B", "C", "D" }; // convert array to a list List<String> list = convertToList(arr); // print list System.out.println(list); } } |
Output:
[A, B, C, D]
Collectors.toList() doesn’t guarantee the type of the list returned. We can use Collectors.toCollection() instead where we can specify the desired Collection. For example,
|
1 2 3 4 |
// Generic method to convert an array to an `ArrayList` public static <T> List<T> convertToList(T[] arr) { return Arrays.stream(arr).collect(Collectors.toCollection(ArrayList::new)); } |
5. Using Guava Library
We can also use Guava API to convert an array to a list.
1. Lists.newArrayList() creates a mutable ArrayList instance containing the given elements. We should use this method to add or remove elements later, or some of the elements can be null.
|
1 2 3 4 |
// Generic method to convert an array to an `ArrayList` public static <T> List<T> convertToList(T[] arr) { return Lists.newArrayList(arr); } |
2. Guava also provides several simple, easy-to-use immutable versions of List. We can use ImmutableList.copyOf or ImmutableList.of() that returns an immutable list containing the elements of the specified array. These should be preferred where mutability is not required.
|
1 2 3 4 |
// Generic method to convert an array to a list public static <T> List<T> convertToList(T[] arr) { return ImmutableList.copyOf(arr); } |
That’s all about converting an array to a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)