Sometimes, I need to create a Java List containing only one object.


Use the static asList method in java.util.Arrays class:


It takes a varargs parameter so I can provide multiple objects. The JavaDoc for this API says:

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

Returns a fixed-size list backed by the specified array. (Changes to the returned list “write through” to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray().