Enum reverse map Java

Since there was a purpose to reverse Enum itself from the value of Enum, I will leave it as a memorandum.

Implementation example in Enum with String value as value

public enum Mode {
   MODEA("a"),
   MODEB("b");

   private String mode;

   private Mode(String mode) {
       this.mode = mode;
   }

   /**Create a reverse map in advance and bring it in the field*/
   private static final Map<String, Mode> modeMap = new HashMap<String, Mode>() {
      private static final long serialVersionUID = 1L;
      {
           for (Mode mode : Mode.values()) {
               put(mode.mode, mode);
           }
       }
   };

   /**Get Enum from value*/
   public static Mode getMode(String mode) {
       return modeMap.get(mode);
   }
} 

User side

Mode modeA = Mode.getMode("a");

You can create a reverse map in advance and get an Enum using String as a key. Since it is a map, it can be accessed directly and processing efficiency is good.

Recommended Posts

Enum reverse map Java
JAVA (Map)
[Java] enum
[Java] Map comparison
Reverse Key from Value in Java Map
Java Enum utilization example
Java bidirectional map library
[Java] About enum type
Reverse Enum constants from strings and values in Java
[Java] How to use Map
[Java] How to use Map
How to use Java Map
Enum Strategy pattern in Java
Reproduce Java enum in C #
[Java] Stream (filter, map, forEach, reduce)
[Java] Branch enum with switch statement
[Java] Collection-List / Set / Map / Stack / Queue
Use composite keys in Java Map.
[Java] Convert 1-to-N List to Map
Java
[Java] Reduce if statements with Enum
Formatting an enum using formatter-maven-plugin (Java)
Java
Java8 list conversion with Stream map
How to use Java enum type
[Java] Express Enum type without using Enum (enumeration) type
[Java] Get List / Map elements with Iterator
Duplicate Map sorted by key in Java
Map without using an array in java
Make something like Java Enum with Typescript
Consider searching Java Enum by another element
[Java] Map # merge is hard to understand.