Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy collection literals?
    primarykey
    data
    text
    <p>From the various online articles on Java 7 I have come to know that Java 7 will be having collection literals<sup>1</sup> like the following:</p> <pre><code>List&lt;String&gt; fruits = [ "Apple", "Mango", "Guava" ]; Set&lt;String&gt; flowers = { "Rose", "Daisy", "Chrysanthemum" }; Map&lt;Integer, String&gt; hindiNums = { 1 : "Ek", 2 : "Do", 3 : "Teen" }; </code></pre> <p>My questions are:</p> <ol> <li>Wouldn't it have been possible to provide a static method <code>of</code> in all of the collection classes which could be used as follows:</li> </ol> <p><code>List&lt;String&gt; fruits = ArrayList.of("Apple", "Mango", "Guava");</code></p> <p>IMO this looks as good as the literal version and is also reasonably concise. Why then did they have to invent a new syntax (<strong>EDIT:</strong> By 'new' I mean 'new to Java'.)?</p> <ol start="2"> <li>When I say <code>List&lt;String&gt; fruits = [ "Apple", "Mango", "Guava" ];</code> what <code>List</code> would I actually get? Would it be <code>ArrayList</code> or <code>LinkedList</code> or something else?</li> </ol> <hr> <p><sup>1</sup> As noted in the comments, collection literals did <em>not</em> make the cut for Java 7, nor indeed Java 8. (Here's an <a href="http://mail.openjdk.java.net/pipermail/lambda-dev/2014-March/011938.html" rel="nofollow noreferrer">email</a> from Brian Goetz, an Oracle developer, summarizing the rationale for not including this feature; and here is the <a href="http://openjdk.java.net/jeps/186" rel="nofollow noreferrer">proposal</a> itself.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload