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 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.

4 Comments

  1. Juan said:

    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:

    
    public static PageType valueOf(int pageType) {
            return (PageType)map.get(pageType);   //CAST HERE
    }
    
    

    That’s how I make it working, thanks again!
    Regards,
    Juan

    November 2, 2016
    Reply
    • Bo Andersen said:

      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! :-)

      November 6, 2016
      Reply
  2. Alex said:

    And what about using this approach:

        public int toInt() {
            return Arrays.binarySearch(values(), this);
        }
    
        public static SomeEnumType fromInt(int i) {
            SomeEnumType[] values = values();
            if (i = values.length) {
                throw new IllegalArgumentException("Out of index!");
            }
            return values[i];
        }
    
    December 2, 2016
    Reply
    • Alex said:

      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.

      December 5, 2016
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *