Java Enum with Multiple Values: Fields and Reverse Lookup
Create a Java enum with multiple values per constant, read them with getters, and find the constant from any value with a cached map and Optional.
Create a Java enum with multiple values per constant, read them with getters, and find the constant from any value with a cached map and Optional.
Learn to create a Java enum with String values, convert an enum to a String with name(), toString() or a getter, and convert a String back to an enum safely with valueOf() or a case-insensitive lookup that returns Optional.
An enum is a data type with a fixed set of named constants. This Java enum tutorial covers declaring enums, values(), valueOf(), ordinal() and name(), switch and pattern matching on Java 25, constructors, fields and methods, EnumSet, EnumMap, singletons and safe String-to-enum conversion.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.