tags:

views:

568

answers:

2

Hi,

I have defined my Enums like this.

public enum UserType {

    RESELLER("Reseller"),

    SERVICE_MANAGER("Manager"),

    HOST("Host");

    private String name;

    private UserType(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

What should be the easiest way to get a key-value pair form the enum values?

The output map i want to create should be like this

key   = Enum(example:- HOST)
value = Host

The map I want do define is

Map<String, String> constansts = new HashMap<String, String>();

Ans: What I Did

I have created a Generic Method to access any enum and change values from that to a Map. I got this IDEA, form a code fragment found at here in any other thread.

public static <T extends Enum<T>> Map<String, String> getConstantMap(
        Class<T> klass) {

    Map<String, String> vals = new HashMap<String, String>(0);

    try {
        Method m = klass.getMethod("values", null);
        Object obj = m.invoke(null, null);

        for (Enum<T> enumval : (Enum<T>[]) obj) {
            vals.put(enumval.name(), enumval.toString());
        }

    } catch (Exception ex) {
        // shouldn't happen...
    }

    return vals;
}

Now, After this all I have to do is call this method with all the enum constructs and i am Done.

One More thing

To get This done i have to orverride the toString Method like this

    public String toString() {
        return name;
    }

Thanks.

+6  A: 

Provided you need to map from the textual values to the enum instances:

Map<String, UserType> map = new HashMap<String, UserType>();
map.put(RESELLER.getName(), RESELLER);
map.put(SERVICE_MANAGER.getName(), SERVICE_MANAGER);
map.put(HOST.getName(), HOST);

or a more generic approach:

for (UserType userType : UserType.values()) {
    map.put(userType.getName(), userType);
}
Péter Török
This is the very HARD way indeed. Don't there exists any thing out of box? There must be something........
vijay.shad
@vijay: how is this hard? Why do you need that in the first place?
Joachim Sauer
@vijay.shad see my update. If you give more details in your question, you increase the chances of getting exactly what you are looking for :-)
Péter Török
There is no easier way given to you by Java... sorry
mlaverd
How is a loop containing one line (three lines total including the closing brace if you use them) the "very HARD way indeed"?
CPerkins
The point is: I may need to come back to code and update the map; every time a user type is added in the enum. I have used enum to define my constants and I want to access then as a Map.
vijay.shad
@vijay: **why** do you need it as a Map? Are you aware of the `valueOf()` method provided by all enums? What do you want to solve that's not solved by `UserType.valueOf(someString).getName()`?
Joachim Sauer
For clarification: Vijay's first comment is about an earlier version of my answer, which did not yet contain the second solution with the loop. I believe the latter version satisfies his requirement.
Péter Török
+4  A: 

You could use the values() method on the enum, giving you all possible combinations, and put that in a map using the iterator.

Map<String, UserType> map = new HashMap<String, UserType>();
for (UserType userType : UserType.values()) {
    map.put(userType.name(), userType);
}
extraneon