I need an EnumSet
from an array (which is given through a varargs method parameter). First, I was surprised that there is no varargs constructor method in EnumSet
(there is EnumSet#of(E first, E... rest)
). As a workaround, I used the following variant:
EnumSet<Options> temp = EnumSet.copyOf(Arrays.asList(options));
However, this triggers a java.lang.IllegalArgumentException: Collection is empty
. So, now I ended up with the following, which looks somewhat ridiculous:
EnumSet<Options> temp = options.length > 0 ?
EnumSet.copyOf(Arrays.asList(options)) :
EnumSet.noneOf(Options.class);
If course this could be moved to some utility method, but still, I'm asking myself if there is a simpler way using existing methods?