Why collection literals ?

Posted by Green Hyena on Stack Overflow See other posts from Stack Overflow or by Green Hyena
Published on 2010-04-23T17:48:48Z Indexed on 2010/04/23 17:53 UTC
Read the original article Hit count: 426

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about java7