Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting values of enumerations, and parsing string to get an enum

My enum is like this currently:

public enum Manufacturers {
  Honda,
  GM,
  Toyota,
  Ferrari
}

I need to create a Hashmap so I plan on doing this, is this correct?

Manufacturers mfg = Manufacturers.Honda;

mfg.ordinal()  // save as key

i.e. I will store the key using the enumInstance.ordinal()

Also, I need to be able to parse a string which will be the ordinal value of the enumeration, and get an enum back:

Manufacturers mfg = Manufacturers.valueOf(mfgOrdinalValueAsString);

The above gave me an error (the string was "1"). Is this the correct way? I guess I should have a try/catch in there right?

like image 240
Blankman Avatar asked Oct 09 '22 07:10

Blankman


2 Answers

The .valueOf would actually be expecting the String "GM" (for 1).

As for storing your enum values in a map, use EnumMap which is designed specifically for this - and will be fast at it, too.

If you really wanted to reference a value by its ordinal, use something like Manufacturers.values()[1].

like image 121
ziesemer Avatar answered Oct 13 '22 12:10

ziesemer


A suggestion: better use name() to get the name of the enum as a String, and whenever you need to get back the original Enum from it, use the valueOf() method - since valueOf() expects the name, not the ordinal, as a parameter. For example:

enum Example {ONE, TWO};

String name = Example.ONE.name();
Example e = Example.valueOf(Example.class, name);  // e has value ONE

If you definitely need to use the ordinal, the ordinal() method will return an index which you can use to retrieve the respective Enum from the array returned by the values() method. Like this:

int ordinal = Example.ONE.ordinal();
Example e = Example.values()[ordinal];  // e has value ONE

As has already been pointed out, consider using EnumMap, as stated in the documentation, it is

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

EDIT

If you need to associate a different code to each element of the enum (other than its automatically assigned ordinal), you can always add it as an attribute to the enum, together with getters and setters, like this:

public enum Manufacturers {

    Honda(10),
    GM(20),
    Toyota(30),
    Ferrari(40);

    private int code;

    Manufacturers(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

}

For example:

Manufacturers m = Manufacturers.Honda;
System.out.println(m.getCode()); // prints 10
m.setCode(100);
System.out.println(m.getCode()); // prints 100

Just be aware that you won't be able to reconstruct an Enum object from the code attribute, since that was defined by the programmer.

like image 25
Óscar López Avatar answered Oct 13 '22 10:10

Óscar López