June 2018
Beginner
722 pages
18h 47m
English
It is possible to use a double brace initializer for the collection initialization. It fits especially well when the collection is the value of an instance field, so it is initialized automatically during object creation. Here is an example:
public class ManageCollections { private List<String> list = new ArrayList<>() { { add(null); add("s2"); add("s3"); } }; public List<String> getThatList(){ return this.list; } public static void main(String... args){ ManageCollections mc = new ManageCollections(); System.out.println(mc.getThatList()); //prints: [null, s2, s3] }}
We have added a getter and use it when the main() method runs. Unfortunately, the double brace initializer does not save any time typing compared ...
Read now
Unlock full access