A common scenario where converting an array to a list is required is given below.

Suppose you have an array of integers or string elements and you want to invoke a method of some external library which only accepts a list argument.

In such case, conversion of array to a list is required.

This post demonstrates different ways by which an array can be converted into a List.

Method 1 : By iterating over the array
Initialize an empty list for holding the elements of the array.
Iterate over the array and in each iteration, add the array element to the list.

If the list is of a generic type, then its type must match the type of the array.

That is, if the array is of type int, then the list should be declared as List<Integer>. If the array is of type char then the list should be declared as List<Character> and so on.
Non-generic list declarations are also permitted as shown in the code below.

List list = new ArrayList();
int array = {12, 45, 10, 4, 5};
for(int counter = 0; counter < array.length; counter++) {
    list.add(array[counter]);
}
System.out.println(list);

Output

[12, 45, 4, 5]

Method 2 : Using toList method of Arrays class

java.util.Arrays class has a static toList method which takes an array as argument and returns a List representation of this array.
Note that since a list cannot be of the primitive type so the statement :

List list = Arrays.asList(array);

will be a compiler error.
A list can only hold objects, so the following conversions are valid :

  1. List<int[]> list = Arrays.asList(array);
  2. List list = Arrays.asList(array);

Thus the following code converts an array into a list :

int[] array = {12, 45,10, 4, 5};
List list = Arrays.asList(array);
System.out.println(list);

Output

[12, 45, 10, 4, 5]

Method 3 : Using stream in java 8

Java streams can be utilized to convert an array to a List.
A new static method stream is added to java.util.Arrays class in java 8.
This method takes the array as argument and returns a stream to this array.
InvokeĀ collect method on this stream.
collect takes a java.util.stream.Collector as argument.
Collector provides methods to add the referred stream elements into a collection such as a List, Set or a Map.
This, an array can be convertedĀ  to a list as :

int[] array = {12, 45,10, 4, 5};
List list = Arrays.stream(nums).boxed().collect(Collectors.toList());
System.out.println(list);

Output

[12, 45, 10, 4, 5]

As explained above, a primitive array can not be converted into a list of primitives.
Thus, boxed method has been called on the stream to convert primitive int values to their corresponding object representation.
If the array is of type Integer then boxed method is not required.

Let’s tweak in :

  1. Using all above methods, an array of any type can be converted to a list of the corresponding type.
    For Example, an array of float can be converted to a list of floats, array of chars can be converted to a list of characters and so on.
  2. Boxing is the automatic conversion of primitive type to its corresponding wrapper type.
    It is due to boxing that we can write a statement such as Integer i = 10;
  3. boxed method only needs to be called when the array passed to the stream is of primitive type.
    There will be a compiler error, if :

    • The array is of primitive type and it is assigned to a List and boxed method is not called.
    • The array is of wrapper type as Integer[] and boxed method is called.

Do not forget to hit the clap if the article was useful.

1
Liked the article ? Spread the word...

Leave a Reply