You can use the utility method Arrays.asList
and feed that result into a new ArrayList
.
List<String> list = new ArrayList<String>(Arrays.asList(s));
Other options:
List<String> list = new ArrayList<String>(Collections.nCopies(1, s));
and
List<String> list = new ArrayList<String>(Collections.singletonList(s));
ArrayList(Collection)
constructor.Arrays.asList
method.Collections.nCopies
method.Collections.singletonList
method.With Java 7+, you may use the "diamond operator", replacing new ArrayList<String>(...)
with new ArrayList<>(...)
.
Java 9
If you're using Java 9+, you can use the List.of
method:
List<String> list = new ArrayList<>(List.of(s));
Regardless of the use of each option above, you may choose not to use the new ArrayList<>()
wrapper if you don't need your list to be mutable.