How to convert a List to an Array in Java

In our previous tutorial, we have discussed various ways of converting an Array to a List in java7 as well as in Java 8. but there are scenarios when we have to change a List to an Array. In this tutorial, we will discuss on various possible ways on how to convert a List to an Array.

Approaches that we will discuss today are –

  • List.toArray()
  • Using Java 8 streams API

Using List.toArray()

This method has two overloaded methods-

  • <T> T[] toArray(T[] a)
  • Object[] toArray()

If we know what is the input type and what is the output type then we use the first declaration and pass the array of the same type, else we call the second method which returns an array of Object.

As per the official documentation of this method –

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

So as it suggested input array is mainly used to know the type of data because if array is smaller or larger this method will take care itself.

Lets understand it via an example 

import java.util.List;

public class ListToArrayExample {
  public static void main(String[] args) {
    // List.of() was introduced in Java 9
    List<String> list = List.of("Harry Potter", "Lord Voldemort", "Hedwig");

    System.out.println("Contents of list ::" + list);

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

    for (int i = 0; i < convertedArray.length; i++) {
      System.out.println("Element at the index " + i + " is ::" + convertedArray[i]);
    }
  }
}
Output:-
Contents of list ::[Harry Potter, Lord Voldemort, Hedwig]
Element at the index 0 is ::Harry Potter
Element at the index 1 is ::Lord Voldemort
Element at the index 2 is ::Hedwig

Using Java Streams

Java 8 streams also provide a simple way to transform a list to an array.
In this, we create a stream of list, perform operations like filtering and get the array using the terminal method toArray().

Have a look at example on how to use Java 8 streams to convert list to Array.

import java.util.List;

public class ListToArrayUsingStreams {
  public static void main(String[] args) {
    List<String> list = List.of("Harry Potter", "Harry Potter 2", "Lord Voldemort", "Hedwig");

    System.out.println("Contents of list ::" + list);

    // filters name which starts from Harry only
    String[] convertedArray = list.stream().filter(name -> name.startsWith("Harry")).toArray(String[]::new);

    for (int i = 0; i < convertedArray.length; i++) {
      System.out.println("Element at the index " + i + " is ::" + convertedArray[i]);
    }
  }
}
Output:-
Contents of list ::[Harry Potter, Harry Potter 2, Lord Voldemort, Hedwig]
Element at the index 0 is ::Harry Potter
Element at the index 1 is ::Harry Potter 2

Do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.

Share and subscribe.

Keep Coding!! Happy Coding!! 🙂

References – 

Recommended -