Java Utililty Methods EnumSet Create

List of utility methods to do EnumSet Create

Description

The list of methods to do EnumSet Create are organized into topic(s).

Method

EnumSetasSet(E[] array)
as Set
return array != null ? asSet(Arrays.asList(array)) : null;
EnumSetcreateEnumSet(Class elementType)
create Enum Set
if (elementType == null) {
    return null;
return EnumSet.allOf(elementType);
EenumFromSet(EnumSet theSet)
enum From Set
if (theSet.isEmpty()) {
    return null;
} else {
    return theSet.iterator().next();
EnumSetenumSet(T... elements)
Terse (especially as a static import) way to create an EnumSet.
return EnumSet.<T>of(elements[0], elements);
EnumSetenumSetOf(long bitSet, Class eClass)
enum Set Of
if (bitSet == EMPTY_BIT_SET) {
    return EnumSet.noneOf(eClass);
EnumSet<E> flagSet = EnumSet.noneOf(eClass);
for (E f : eClass.getEnumConstants()) {
    if (hasEnum(bitSet, f)) {
        flagSet.add(f);
return flagSet;
SetfromString(Class enumType, String value)
from String
final Set<E> enumValues = new HashSet<E>();
final StringTokenizer tokens = value == null ? null : new StringTokenizer(value);
if (tokens == null || !tokens.hasMoreTokens()) {
    return EnumSet.noneOf(enumType);
for (; tokens.hasMoreTokens();) {
    try {
        final E enumValue = Enum.valueOf(enumType, tokens.nextToken());
...
TfromString(Class enumClass, String name)
Parses an Enum of type T case insensitively.
if (name == null) {
    return null;
EnumSet<T> values = EnumSet.allOf(enumClass);
for (T value : values) {
    if (value.name().equalsIgnoreCase(name)) {
        return value;
int index = Integer.parseInt(name);
if (index < values.size() && index >= 0) {
    return (T) values.toArray()[index];
throw new IllegalArgumentException("No enum constant " + enumClass.getCanonicalName() + "." + name);
EnumSetintToEnumSet(Class enumClass, int decoded)
Get an enum set by parsing an integer which represents a bit array.
EnumSet<T> enumSet = EnumSet.noneOf(enumClass);
T[] enums = enumClass.getEnumConstants();
while (decoded != 0) {
    int ordinal = Integer.numberOfTrailingZeros(decoded);
    enumSet.add(enums[ordinal]);
    decoded -= Integer.lowestOneBit(decoded);
return enumSet;
...
EnumSetnewEnumSet(Class klass)
new Enum Set
return EnumSet.noneOf(klass);
EnumSetnoneOf(Class elementType)
none Of
return EnumSet.noneOf(elementType);