EnumByName<T extends Enum> extension

Access enum values by name.

Extensions on a collection of enum values, intended for use on the values list of an enum type, which allows looking up a value by its name.

Since enum classes are expected to be relatively small, lookup of byName is performed by linearly iterating through the values and comparing their name to the provided name. If a more efficient lookup is needed, perhaps because the lookup operation happens very often, consider building a map instead using asNameMap:

static myEnumNameMap = MyEnum.values.asNameMap();

and then use that for lookups.

on
Annotations
  • @Since("2.15")

Methods

asNameMap() Map<String, T>

Available on Iterable<T>, provided by the EnumByName extension

Creates a map from the names of enum values to the values.
byName(String name) → T

Available on Iterable<T>, provided by the EnumByName extension

Finds the enum value in this list with name name.