Convert List to array in Java

Problem

Given the following List:

List<Integer> list = Arrays.asList(1, 2, 3);

How to convert this List to an array?

Solution

Java 8 and above

Integer[] array = list.toArray(Integer[]::new);

Java 6 and above

Integer[] array = list.toArray(new Integer[0]);