Fixed size List
The easiest way, that I know of, is to create a fixed-size single element List
with Arrays.asList(T...)
like
// Returns a List backed by a varargs T.
return Arrays.asList(s);
Variable size List
If it needs vary in size you can construct an ArrayList
and the fixed-sizeList
like
return new ArrayList<String>(Arrays.asList(s));
and (in Java 7+) you can use the diamond operator <>
to make it
return new ArrayList<>(Arrays.asList(s));
Single Element List
Collections can return a list with a single element with list being immutable:
Collections.singletonList(s)
The benefit here is IDEs code analysis doesn’t warn about single element asList(..) calls.