Proposal: Collection Literals

Kevin Bourrillion kevinb at
Tue Mar 31 15:21:54 PDT 2009

On Tue, Mar 31, 2009 at 9:55 AM, Joshua Bloch <jjb at> wrote:

> Kris,
> Actually my proposal was intended to address creation of mutable
> collections.  I should have mentioned this explicitly in the proposal, and
> will do so.  Here's the idiom I recommend.  Suppose you want a
> LinkedHashSet
> initialized to contain Santa's eight reindeer.  This will do the trick:
>  Set<String> team = new LinkedHashSet<>(
>    ["Dasher", "Dancer", "Prancer", "Vixen","Comet", "Cupid", "Donner",
> "Blitzen"]);

This is 100% the right idea!

I'll also note that use cases for prepopulating collections that need to be
mutable are very rare compared to the cases for immutable collections and
the cases where you don't care (and so immutability is the sensible default

I'd like everyone to consider importing the Immutable collections from our
Google Collections Library.  Example:

On top of the advantages cited by Jesse (a few of which are admittedly
shared by other alternatives), consider that the memory consumption of
ImmutableSet is (size + tablesize), compared to (8 * size + tablesize) for
its mutable equivalent (LinkedHashSet)!

We wrote these collections with the specific goal in mind that they should
be the ideal runtime type for a collection literal.

Last point:  Josh's proposed feature would allow me to stop using varargs in
APIs completely, and avoid the ugly problems involved with them (overload
confusion, forcing to the last argument position, having to deal with an
array instead of a list (or worse, the 'first, second, rest' problem!) in my
implementation, etc.

Kevin Bourrillion @ Google
internal:  http://go/javalibraries

More information about the coin-dev mailing list