Enum to Integer and Integer to Enum in Java

Published on November 5, 2015 by

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.

Featured

Learn Java today!

Take an online course and become a Java champion!

Here is what is covered:

  • How to build complete programs using the latest version of Java
  • Writing advanced cross-platform applications
  • How to conform with best practices
  • Advanced Java concepts
  • OOP, generics, collections, JavaFX, concurrency, lambda, databases, networking, etc.
Java logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

4 comments on »Enum to Integer and Integer to Enum in Java«

  1. Juan

    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

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

  2. Alex

    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];
    }
    • Alex

      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.

Leave a Reply

Your e-mail address will not be published.