Java ArrayList to Array
In this article, we will understand 3 ways to convert a java ArrayList to array with example programs.

One is a simpler approach by iterating over the list, second is by using java api methods and third uses java 8 streams.
One of the most common scenarios where you need to convert a java Arraylist to array is when calling a method from a third party library which accepts an array while the code calling this method needs to pass an ArrayList.

Method 1 : Iterating over the ArrayList
This is a simpler approach which involves creating an array equal to the size of the list, iterating the list and adding element at every index of the list to the array.
Remember, the array should be of the same or compatible type as the type of ArrayList otherwise there may be compiler error or a java.lang.ClassCastException at runtime.

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

public class ListToArrayConverter {
    public static void main(String[] args) {
        // create the list
        List list = new ArrayList();
        // add elements to list
        list.add("Java");
        list.add("C#");
        list.add("Ruby");
        list.add("VB.NET");   
        list.add("Python");
        // get the size of list
        int size = list.size();
        // create an array of same size  
        String[] languages = new String[size]; 
        for(int i = 0; i < size; i++) {
           // get the element at current list index to the array   
           languages[i] = list.get(i);
        }
    }
}

Output

Java
C#
Ruby
VB.NET


Method 2
: Using toArray() method
java.util.List has a toArray() method which takes an array as argument and populates this array with the contents of Arraylist.
The type of this array must be same as the type of List. Also, the size of the array should be of the same size as that of the list.

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

public class ListToArrayConverter {
    public static void main(String[] args) {
        // create the list
        List list = new ArrayList();
        // add elements to list
        list.add("Java");
        list.add("C#");
        list.add("Ruby");
        list.add("VB.NET");   
        list.add("Python");
        // get the size of list
        int size = list.size();
        // create an array of same size  
        String[] languages = new String[size]; 
        // convert list to array
        list.toArray(languages); 
        // print array contents 
        for(int i = 0; i < size; i++) {
           System.out.println(languages[i]);
        } 
    }
}

Output

Java
C#
Ruby
VB.NET

Code to convert list to array given above can be written in one line as

list.toArray(new String[list.size()]);

Method 3: Using Java 8
Java 8 streams can be used to convert a java ArrayList to array. Below is the code example.

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

public class ListToArrayConverter {
  public static void main(String[] args) {
    // create a list
    List<Integer> list = new ArrayList<>();
    // add elements
    list.add(3);
    list.add(1);
    list.add(19);
    list.add(190);
    list.add(102);
    // convert to array
    Integer[] array = list.
                      stream().
                      map(e -> e).
                      toArray(Integer[]::new);
    // print array contents 
    System.out.println("Array elements are:");
    for (Integer integer : array) {
       System.out.print(integer + " ");
    } 
  }
}

A stream has map() method which takes a java.util.function.Function argument. This is a Functional interface having a single method that accepts one argument and returns a result.

So, if we implement this method such as it returns the same element which is supplied to it as argument and add it to an array using toArray method of the stream, then the list will be converted to an array.

Above example is doing the same thing.
Argument to map method is implemented as a Lambda expression since it accepts a Functional interface.
Output of this program is

Array elements are:
3 1 19 190 102

This example prints the array using enhanced for loop but you also use other ways to print an array.

Refer this post to learn different ways to convert array to list in java.


Let’s tweak in

  1. If the type of array supplied to toArray() method is different from the type of list, then a java.lang.ArrayStoreException is raised at run time.
  2. If the array supplied to the toArray is null then java.lang.NullPointerException is raised.
  3. If the size of the array is greater than the elements in the list, then the remaining excess elements of the array will be null.
  4. If the size of the array is lesser than the elements in the list, then all the elements of the array are null.
  5. toArray() method is declared in java.util.List which is an interface and implemented in java.util.ArrayList.

Hit the clap below if the article was useful.

0
Liked the article ? Spread the word...

Leave a Reply