Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting enum associated with int value

Tags:

java

enums

Previously, I had my LegNo enums defined simply as:

NO_LEG, LEG_ONE, LEG_TWO 

and by calling return LegNo.values()[i];, I was able to get the value associated with each enum.

But now I've decided I want the LegNo enum NO_LEG to be the int -1 instead of 0 so I decided to use a private constructor to initialise and set its int value

NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);  private LegNo(final int leg) { legNo = leg; } 

the only thing now is that because I'm doing it this way the values() method will not work for the NO_LEG enum. How do I get the enum associated with the int? Is there any efficient way of doing this other than using a case switch statement or an if-elseif-elseif

I can see a lot of SO questions to do with getting the int value from the enum, but I'm after the reverse.

like image 637
L-Samuels Avatar asked Jun 15 '12 09:06

L-Samuels


People also ask

Can enum have int values?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

Can we get an enum by value?

Get the value of an EnumTo get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.


2 Answers

EDIT August 2018

Today I would implement this as follows

public enum LegNo {     NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);      private final int value;      LegNo(int value) {         this.value = value;     }      public static Optional<LegNo> valueOf(int value) {         return Arrays.stream(values())             .filter(legNo -> legNo.value == value)             .findFirst();     } } 

You'll have to maintain a mapping inside the enum.

public enum LegNo {     NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);      private int legNo;      private static Map<Integer, LegNo> map = new HashMap<Integer, LegNo>();      static {         for (LegNo legEnum : LegNo.values()) {             map.put(legEnum.legNo, legEnum);         }     }      private LegNo(final int leg) { legNo = leg; }      public static LegNo valueOf(int legNo) {         return map.get(legNo);     } } 

The static block will be invoked only once, so there is practically no performance issue here.

EDIT: Renamed the method to valueOf as it is more inline with other Java classes.

like image 169
adarshr Avatar answered Oct 09 '22 04:10

adarshr


You could also include a static method in the enum that iterates through all members and returns the correct one.

public enum LegNo {    NO_LEG(-1),    LEG_ONE(1),    LEG_TWO(2);     private int legIndex;     private LegNo(int legIndex) { this.legIndex = legIndex; }     public static LegNo getLeg(int legIndex) {       for (LegNo l : LegNo.values()) {           if (l.legIndex == legIndex) return l;       }       throw new IllegalArgumentException("Leg not found. Amputated?");    } } 

Now, if you want to get an Enum value by the integer, you just use:

int myLegIndex = 1; //expected : LEG_ONE LegNo myLeg = LegNo.getLeg(myLegIndex); 
like image 21
Mike Adler Avatar answered Oct 09 '22 04:10

Mike Adler