How to convert List to array in Java?

To convert List to array in Java, do this: list.toArray(String[]::new).

Here's how you do it:

java.util.List<String> list = java.util.List.of("Hello", "World", "!");
String[] array = list.toArray(String[]::new);

// [Hello, World, !]
System.out.println(java.util.Arrays.toString(array));