You can also use the varargs syntax to make your code cleaner:
Use the overloaded constructor:
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c"));
Subclass ArrayList in a utils module:
public class MyArrayList<T> extends ArrayList<T> {
public MyArrayList(T... values) {
super(Arrays.asList(values));
}
}
ArrayList<String> list = new MyArrayList<String>("a", "b", "c");
Or have a static factory method (my preferred approach):
public class Utils {
public static <T> ArrayList<T> asArrayList(T... values) {
return new ArrayList<T>(Arrays.asList(values));
}
}
ArrayList<String> list = Utils.asArrayList("a", "b", "c");