Quantcast
Channel: Initialization of an ArrayList in one line - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Answer by Naman for Initialization of an ArrayList in one line

$
0
0

With and above, as suggested in JEP 269: Convenience Factory Methods for Collections, this could be achieved using collection literals now with -

List<String> list = List.of("A", "B", "C");Set<String> set = Set.of("A", "B", "C");

A similar approach would apply to Map as well -

Map<String, String> 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 languageproposals were set aside in preference to a library-based proposal assummarized in this message.

Related: What is the point of overloaded Convenience Factory Methods for Collections in Java 9


Viewing all articles
Browse latest Browse all 37

Trending Articles