I would like to fill a list with all possible values of an enum
Since I recently fell in love with EnumSet, I leveraged allOf()
EnumSet all = EnumSet.allOf( Something.class);
List list = new ArrayList<>( all.size());
for (Something s : all) {
list.add( s);
}
return list;
Is there a better way (as in non obfuscated one liner) to achieve the same result?
解决方案
I wouldn't use a List in the first places as an EnumSet is more approriate but you can do
List somethingList = Arrays.asList(Something.values());
or
List somethingList =
new ArrayList(EnumSet.allOf(Something.class));