In this tutorial, I am going to show you how to convert Java ArrayList to Array.

Java ArrayList to Array :

We can convert an ArrayList to array in below ways.

Java list.toArray()

list.toArray() returns array of objects (Object[]).

ArrayListToArray.java
import java.util.ArrayList;

public class ArrayListToArray {

    public static void main(String[] args) {
        ArrayList<String> course = new ArrayList<String>();
        course.add("Java");
        course.add("Spring");
        course.add("Hibernate");
        course.add("Angular");
        course.add("PHP");
        Object[] courseArray = course.toArray();
        System.out.println("courseArray.length = " + courseArray.length);

    }

}

Output:

Terminal
courseArray.length = 5

Java list.toArray(T[])

We can define the type of an array while converting like below.

ArrayListToArray.java
import java.util.ArrayList;

public class ArrayListToArray {

    public static void main(String[] args) {
        ArrayList<String> course = new ArrayList<String>();
        course.add("Java");
        course.add("Spring");
        course.add("Hibernate");
        course.add("Angular");
        course.add("PHP");
        Object[] courseArray = course.toArray(new String[course.size()]);
        System.out.println("courseArray.length = " + courseArray.length);

    }

}

Output:

Terminal
courseArray.length = 5

Java 8 ArrayList to Array :

ArrayListToArray.java
import java.util.ArrayList;

public class ArrayListToArray {

    public static void main(String[] args) {
        ArrayList<String> course = new ArrayList<String>();
        course.add("Java");
        course.add("Spring");
        course.add("Hibernate");
        course.add("Angular");
        course.add("PHP");
        String[] courseArray = course.stream().toArray(String[]::new);
        System.out.println("courseArray.length = " + courseArray.length);

    }

}

Output:

ArrayListToArray.java
courseArray.length = 5

Happy Learning 🙂