list literal gotcha and suggestion

Florian Weimer fw at deneb.enyo.de
Wed Sep 30 12:32:54 PDT 2009


* Greg Brown:

> classes? To me, this:
>
> List<String> list = ["a", "b", "c"];
>
> isn't much better than:
>
> List<String> list = new ArrayList<String>("a", "b", "c");

But this doesn't work:

    List<Integer> list = new ArrayList<Integer>(1);

So it has to be something like Collections.list() (the result of
Arrays.asList() can't grow).

> I recognize that maps are trickier, but I still think that this:
>
> Map<String, Integer> map = new HashMap<String, Integer>(new  
> AbstractMap.SimpleImmutableEntry<String, Integer>("a", 0),
> 	new AbstractMap.SimpleImmutableEntry<String, Integer>("b", 1),
> 	new AbstractMap.SimpleImmutableEntry<String, Integer>("c", 2));
>
> is preferable to modifying the language for what (to me) seems like a  
> fairly marginal use case.

Really?  You could write

    Map<String, Integer> map = new HashMap<String, Integer>() {{
        put("a", 0);
        put("b", 1);
        put("c", 2);
    }};

today, but with a slightly surprising overhead, especially if the
parameters aren't constant.



More information about the coin-dev mailing list