JEP 186: Collection Literals

Paul Sandoz paul.sandoz at oracle.com
Tue Jan 14 07:46:10 PST 2014


On Jan 14, 2014, at 3:34 PM, "Millies, Sebastian" <Sebastian.Millies at softwareag.com> wrote:

> I would certainly expect it to be an immutable list, and I believe the proposal of having collection literals makes most sense in the context of persistent collections. This would be in keeping with evolving Java towards a more functional and parallel-friendly programming style, which entails embracing immutability. In contrast, what's to be gained from a shorter version of Arrays.asList() ?  -- Sebastian
> 

Note that Arrays.asList creates a List implementation that cannot be structurally modified; it cannot grow or shrink and is essentially a view over the array passed (directly or indirectly) to it. That is why one sees annoying code like:

  new ArrayList<>(Arrays.asList(1, 2, 3, 4));

or:

  new ArrayList<Integer>() {{ add(1); add(2); add(3); }};

--

One reason why i pause for thought about immutability is List is not a particularly a good interface for an immutable list of elements.

Paul.


More information about the lambda-dev mailing list