How to quickly and conveniently create a one element arraylist [duplicate]

Is there a Utility method somewhere that can do this in 1 line? I can’t find it anywhere in Collections, or List.

public List<String> stringToOneElementList(String s) {
    List<String> list = new ArrayList<String>();
    list.add(s);
    return list;
}

I don’t want to re-invent the wheel unless I plan on putting fancy rims on it.

Well… the type can be T, and not String. but you get the point. (with all the null checking, safety checks…etc)

8 Answers
8

Leave a Comment