0

I'm trying to set up my enum so that it has a list of names and retrieve a particular name given that value.

Example: enum {Vanilla, Chocolate, Strawberry};

Given 0, return Vanilla. Given 1, return Chocolate. Given 2, return Strawberry.

I haven't been able to find any methods that come with the Java Enum class to do this out of the box. I'm thinking of writing my own that dumps the values into an array and then use binary search to return them, given a particular number.

Is there something built in however that would be better?

Thanks!

rsteckly
  • 1,952
  • 3
  • 23
  • 35

7 Answers7

3

You have to implement it in the Enum. Like this:

public enum MyEnum {
  VANILLA(0), CHOCOLATE(1), STRAWBERRY(2);

  private int i;

  private MyEnum(int i) {
     this.i = i;
  }

  public static MyEnum getFoodById(int id) trhows IllegalArgumentException {
     MyEnum result = null;
     switch (id):
       case 0: {
         result = MyEnum.VANILLA;
         break;
       }
       case 1: {
         result = MyEnum.CHOCOLATE;
         break;
       }
       case 2: {
         result = MyEnum.STRAWBERRY;
         break;
       }
       default : { 
          throw new IllegalArgumentException("Invalid id ...");
       }
   }
   return result;
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
3

If your id corresponds to the enum item's position, you can use the built-in values() and ordinal()-Methods.

// to enum
int intValue = 0;
CustomEnum item = CustomEnum.values()[intValue];

// to int
CustomEnum item = CustomEnum.SOME_ITEM;
int intValue = item.ordinal();
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

You can use EnumMap. You can look at here for solution and example.

Sukumar
  • 21
  • 1
1

You can also do it this way:

public enum IntEnum {

Vanilla(0), Chocolate(1), Strawberry(2);

private int value;

IntEnum(int value) {
    this.value = value;
}

public int getValue() {
    return value;
}

public static IntEnum getEnum(int intValue) {
    Integer value = intValue;

    if (value == null)
        throw new IllegalArgumentException();
    for (IntEnum v : values())
        if (value.equals(v.getValue()))
            return v;
    throw new IllegalArgumentException();
}



public static void main(String[] args) {

    System.out.println(IntEnum.getEnum(1));         
}

}

Michael W
  • 3,515
  • 8
  • 39
  • 62
0

Enums do allow for built in functions.

public enum Flavor
{
    VANILLA("Vanilla"),
    CHOCOLATE("Chocolate"),
    STRAWBERRY("Strawberry");

    private final String desc;

    Flavor(String desc)
    {
        this.desc = desc;
    }

    public String getDesc()
    {
        return desc;
    }
}
Bill Clar
  • 139
  • 1
  • 9
0
public enum Flavor {
    Vanilla, Chocolate, Strawberry
}

Flavor v = Flavor.values()[0];

String name = v.name(); // "Vanilla"

http://docs.oracle.com/javase/6/docs/api/java/lang/Enum.html#name()

Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
0

Use the values() result:

enum Flavours {Vanilla, Chocolate, Strawberry}

assertTrue(Flavours.Vanilla == Flavours.values()[0]);
rolfl
  • 17,539
  • 7
  • 42
  • 76