Enum to Integer and Integer to Enum in Java
Need to convert an integer to an enum in Java? The bad news is that it is not as easy as it should be, but the good news is that it is still easy!
Consider the enum below.
public enum PageType {
ABOUT(1),
CODING(2),
DATABASES(3);
private int value;
private static Map map = new HashMap<>();
private PageType(int value) {
this.value = value;
}
static {
for (PageType pageType : PageType.values()) {
map.put(pageType.value, pageType);
}
}
public static PageType valueOf(int pageType) {
return (PageType) map.get(pageType);
}
public int getValue() {
return value;
}
}
With the above enum, one can get the enum from an integer, but also get the integer based on an enum. The trick here is to use a map internally which handles the mapping of an integer (or whichever type you need) to its corresponding enum. This is done in a static method, which iterates through the values within the enum and adds them to the map. There is also a private constructor where we assign the passed value to a variable. This is done automatically upon using an enum due to the parenthesis after each enum name.
Getting Enum from Integer
To get the enum for a given integer, we simply have to call the valueOf method, like below.
PageType pageType = PageType.valueOf(2); // pageType = PageType.CODING
Getting Integer from Enum
On the other hand, to get the integer value from an enum, one can do as follows, by using the getValue method.
ProductType productType = ProductType.DATABASES;
int productTypeId = productType.getValue(); // productTypeId = 3
The getValue method simply returns the internal value of the enum that we store within the value variable.
I hope this helps! Thank you for reading.
15 comments on »Enum to Integer and Integer to Enum in Java«
Thanks for your solution Bo, it really helped me!
Just a comment, I needed to cast the returned object of map.get() to my enum type, in your code it should be:
That’s how I make it working, thanks again!
Regards,
Juan
Hello Juan,
I am happy to hear that this post helped you. Thanks for your positive feedback – and thank you very much for your solution! :-)
And what about using this approach:
Parser cuts several letters. It must be next expression in if block: i < 0 || i >= values.length. There are more and less signs must be.
It seems getting Enum using value of is straightforward.
Thanks,
/Sameera
The remark of @Juan is totally correct. A pity the example code is not adapted accordingly yet…
Hello Geert,
The code has been updated now. Thanks for the reminder! :-)
Really cool guy! :) Good solution!
If you used Map instead of plain Map, there would be no need for the (PageType) cast in the valueOf() method.
pageType.values() from where you magically get “values()” method? It won’t compile
This code returns a int value based of position on array (declared values on enum is an array).
Code:
PageType.CODING.ordinal();
Excellent contribution Andersen.
One observation, If I may, is to check for nullity in your valueOf method, so that you can throw an exception instead of returning null.
Again, thanks for your easy and clean solution!
A better solution to avoid typecasts:
Wrote this implementation. It allows for missing values, negative values and keeps code consistent. The map is cached as well. Uses an interface and needs Java 8.
Enum
Interface
public enum AgrudNewsType {
DIVIDEND(4),CORPORATE_ACTIONS(13);
private int newsTypeId;
private static Map map = new HashMap();
private AgrudNewsType(int newsTypeId) {
this.newsTypeId = newsTypeId;
}
static {
for (AgrudNewsType agrudNewsType : AgrudNewsType.values()) {
map.put(agrudNewsType.newsTypeId, agrudNewsType);
}
}
public static AgrudNewsType valueOf(int agrudNewsType) {
return (AgrudNewsType) map.get(agrudNewsType);
}
public int getNewsTypeId() {
return newsTypeId;
}
}
How do I get the list of all newsTypeIds? like a list of [4,13] in this case?