Java Array to List Conversion Tutorial

In this section we will learn how we can turn an array into list in Java.

Ways to convert Array to List

If you have an array and want to create a list out of that array, there are three methods that can help you with.

  • Arrays asList() Method
  • Collections addAll() Method
  • Arrays stream() Method

What is Java Arrays asList() Method?

The Java Arrays asList() method is a static method and is used to create a fixed sized collection list from an array.

Note: The list is backed by the specified array (that we pass as the argument of this method). So any change either array or the created list does to the elements, the other will see that.

Java Arrays asList() Method Syntax:

public static <T> List<T> asList(T... a)

asList() Method Parameters:

The argument of this method could either be a reference to an array that is already created. Or we can directly put the elements that we want the returned list object of this method to have, as its arguments.

asList() Method Return Value:

The return value of this method is a new list object that contains the elements of the target array object.

asList() Method Exceptions:

The method does not throw an exception.

Example: using Arrays asList() method

import java.util.List; 
import java.util.ArrayList; 
import java.util.Arrays;
public class Main {

    public static void main(String[] args){
        
        Integer i[] = {1,2,3,4,5,6,7}; 
        List<Integer> list = Arrays.asList(i);
       i[0] = 1000;
        for (int ii :list){
         System.out.println(ii);
        }
    }
}

Output:

1000

2

3

4

5

6

7

As you can see, both the array and the created list objects are linked to each other and if one changed the elements, the other will see that change too.

What is Java Collections addAll() Method?

The Java Collections addAll() method is another way of adding the elements of an array to a collection object.

Note: this method copies the elements of an array object and set it as the elements of a collection object. This means the two object elements are independent from each other and changing the value in one object does not affect the other.

Java Collections addAll() Method Syntax:

public static <T> boolean addAll(Collection<? super T> c, T... elements)

addAll() Method Parameters:

This method takes two arguments:

The first argument of the method is a reference to a collection that we want to move elements from an array to there.

The second argument is a reference to an array object we want to copy its elements to a collection.

Note: other than just a reference to an array object, we can put those elements that want to be used in the target collection, directly as the second and next arguments of this method.

addAll() Method Return Value:

The return value of this method is of Boolean type.

If the operation was successful and the array elements were copied into the target collection object, the return value will be true otherwise false.

addAll() Method Exceptions:

UnsupportedOperationException: This exception will be thrown if the target collection object does not support the add operation.

NullPointerException: We will get this exception if we attempt to put null values in the target collection object.

IllegalArgumentException: We get this exception if there’s a property in the elements of the array object that prevents it from being copied into a collection.

Example: using Collections addAll() method

import java.util.List; 
import java.util.ArrayList; 
import java.util.Arrays;
import java.util.Collections;
public class Main {

    public static void main(String[] args){
        
        Integer i[] = {1,2,3,4,5,6,7}; 
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list, i);
        i[0] = 1000;
        for (int ii: list){
         System.out.println(ii);
        }
    }
}

Output:

1

2

3

4

5

6

7

 

As you can see, using the Collections addAll() method copies the elements of an array and put them as the elements of a collection object. This means the array object and the collection are two independent objects and changing the values of one won’t affect the other.

Java Arrays stream() Method

The Java Arrays stream() method is another way of getting the elements of an array and put them for the elements of a collection.

This method takes one argument, and that is a reference to an array object. After that, it will create a stream from the elements of the array and we can then use methods related to streams to take these elements and copy them into a collection object.

Example: using stream() method of Arrays

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

public class Main {

    public static void main(String[] args){
        
        Integer i[] = {1,2,3,4,5,6,7}; 
        List<Integer> list = new ArrayList<>();
        Arrays.stream(i).forEach(e->{
         list.add(e);
        });
        for (int ii: list){
         System.out.println(ii);
        }
    }
}

Output:

1

2

3

4

5

6

7

Leave a Reply