Java 11 introduced an easy way to convert a collection to an array.
Consider the following program −
Java11Feature.java
import java.util.Arrays;
import java.util.List;
public class Java11Feature {
public static void main(String[] args) {
List<String> nameList = Arrays.asList("Elavarasan", "John", "Kumar");
// 1. Old way of convert a collection to an array
String[] names = nameList.toArray(new String[nameList.size()]);
System.out.println(names.length);
// 2. New way of convert a collection to an array
names = nameList.toArray(String[]::new);
System.out.println(names.length);
}
}
Old Way
String[] names = namesList.toArray(new String[nameList.size()]);
New Way
String[] names = nameList.toArray(String[]::new);
Output
3
3