Why collection literals ?
- by Green Hyena
Hi fellow Java programmers.
From the various online articles on Java 7 I have come to know that Java 7 will be having collection literals like the following:
List<String> fruits = [ "Apple", "Mango", "Guava" ];
Set<String> flowers = { "Rose", "Daisy", "Chrysanthemum" };
Map<Integer, String> hindiNums = { 1 : "Ek", 2 : "Do", 3 : "Teen" };
My questions are:
1] Wouldn't it have been possible to provide a static method of in all of the collection classes which could be used as follows:
List<String> fruits = ArrayList.of("Apple", "Mango", "Guava");
IMO this looks as good as the literal version and is also reasonably concise. Why then did they have to invent a new syntax?
2] When I say List<String> fruits = [ "Apple", "Mango", "Guava" ]; what List would I actually get? Would it be ArrayList or LinkedList or something else?
Thanks.