I wanted to create a list of options for testing purposes. At first, I did this:
ArrayList places = new ArrayList();
places.add(\
With java-9 and above, as suggested in JEP 269: Convenience Factory Methods for Collections, this could be achieved using collection literals now with -
List list = List.of("A", "B", "C");
Set set = Set.of("A", "B", "C");
A similar approach would apply to Map
as well -
Map map = Map.of("k1", "v1", "k2", "v2", "k3", "v3")
which is similar to Collection Literals proposal as stated by @coobird. Further clarified in the JEP as well -
Alternatives
Language changes have been considered several times, and rejected:
Project Coin Proposal, 29 March 2009
Project Coin Proposal, 30 March 2009
JEP 186 discussion on lambda-dev, January-March 2014
The language proposals were set aside in preference to a library-based proposal as summarized in this message.
Related: What is the point of overloaded Convenience Factory Methods for Collections in Java 9