Initialize List<> with Arrays.asList

transient_loop picture transient_loop · May 23, 2012 · Viewed 68.8k times · Source

Why does this work:

String[] array = {"a", "b", "c"};
List<String> list = Arrays.asList(array);

but this does not:

List<String> list = Arrays.asList({"a","b","c"});

Answer

Mattias Isegran Bergander picture Mattias Isegran Bergander · May 23, 2012

This is a short hand only available when constructing and assigning an array.

String[] array = {"a", "b", "c"};

You can do this though:

List<String> list = Arrays.asList("a","b","c");

As asList can take "vararg" arguments.