Convert List to Array and Array to List in Java

The Java List interface provides toArray(T[] a) method to convert an object from List type to Array type.

Example:
Prior to Java 8:

List<String> stringList = Arrays.asList("Lion", "Tiger", "Monkey", "Elephant", "Zebra");
String[] stringArray = stringList.toArray(new String[stringList.size()]);
In Java 8:

String[] stringArray = stringList.stream().toArray(String[]::new);
In Java 11:

String[] stringArray = stringList.toArray(String[]::new);

Convert Array to List in Java

The Arrays class of Java provides a static asList(T... a) method that we can use to convert an object of Array type to List type.

Example:

String[] bookArray = {"Science", "English", "Geography", "Economics", "Computer"};
List<String> bookList = Arrays.asList(bookArray);