I have an array that is initialized like:
Element[] array = {new Element(1), new Element(2), new Element(3)};
I would like to convert this
In Java 9, you can use List.of static factory method in order to create a List
literal. Something like the following:
List elements = List.of(new Element(1), new Element(2), new Element(3));
This would return an immutable list containing three elements. If you want a mutable list, pass that list to the ArrayList
constructor:
new ArrayList<>(List.of(// elements vararg))
JEP 269 provides some convenience factory methods for Java Collections API. These immutable static factory methods are built into the List, Set, and Map interfaces in Java 9 and later.