Hidden Gems in Arrays.asList()

I have used the Arrays.asList(Object) method numerous times over the years and I have not needed to look at the documentation for it in a while… apparently not since before Java 5. In Java 5 it has been given a nice new shiny coat of paint with generics and var-args.

public static <T> List<T> asList(T... a)

what this means is that now when you need to create a quick List of something for testing or other purposes, instead of

List<String> strings = Arrays.asList(new String[]{"alpha","bravo","charlie"});

you can simplify it down to…

List<String> strings = Arrays.asList("alpha","bravo","charlie");

Nothing astounding, but interesting and useful all the same. Saves a little typing.

Average: 3.3 (12 votes)