Convert Enum to / from Integer & String value in java

By | March 4, 2016
  • Given an enum in java.
  • Convert enum to integer & vice Versa.
  • Convert enum to string value & vice Versa.

We will performs the following operations on enums

  1. Get enum (Color) as Integer
  2. Get enum (Color) as String
  3. Convert Integer to enum (Color)
  4. Convert String to enum (Color)
  5. Convert enum (Color) to Integer
    • static method to convert enum to Integer
  6. Convert enum (Color) to String
    • static method to convert enum to String

Program- convert Enum to/from Integer & String value

package org.learn;

enum Color {
	RED(10), 
	BLUE(20), 
	GREEN(30), 
	YELLOW(40), 
	BLACK(50);
	
	private final int color;

	Color(int color) {
		this.color = color;
	}

	public int getColorAsInt() {
		return color;
	}

	public String getColorAsString() {
		return String.valueOf(color);
	}

	public static Color convertIntToColor(int iColor) {
		for (Color color : Color.values()) {
			if (color.getColorAsInt() == iColor) {
				return color;
			}
		}
		return null;
	}

	public static Color convertStringToColor(String inputColor) {
		for (Color color : Color.values()) {
			if (color.getColorAsString().equals(inputColor)) {
				return color;
			}
		}
		return null;
	}

	public static int convertColorToInt(Color inputColor) {
		for (Color color : Color.values()) {
			if (color.getColorAsInt() == inputColor.getColorAsInt()) {
				return color.getColorAsInt();
			}
		}
		return -1;
	}

	public static String convertColorToString(Color inputColor) {
		for (Color color : Color.values()) {
			if (color.getColorAsInt() == inputColor.getColorAsInt()) {
				return color.getColorAsString();
			}
		}
		return null;
	}
}

public class EnumTest {
	public static void main(String[] args) {

		Color color = Color.BLACK;
		int iColor = color.getColorAsInt();
		String sColor = color.getColorAsString();
		// Get color as integer value
		System.out.println("1. Get BLACK color as int value :" + iColor);
		// Get color as String value
		System.out.println("2. Get BLACK color as string value :" + sColor);

		color = Color.convertIntToColor(50);
		// Convert integer value to corresponding color value
		System.out.println("3. Convert integer 50 to color :" + color);

		color = Color.convertStringToColor("50");
		// Convert String value to corresponding color value
		System.out.println("4. Convert string 50 to color :" + color);

		iColor = Color.convertColorToInt(Color.BLACK);
		// Convert color value to corresponding integer value
		System.out.println("5. Convert BLACK color to integer :" + iColor);

		sColor = Color.convertColorToString(Color.BLACK);
		// Convert color value to corresponding integer value
		System.out.println("6. Convert BLACK color to String :" + sColor);

	}
}

Output – convert Enum to/from Integer & String value


1. Get BLACK color as int value :50
2. Get BLACK color as string value :50
3. Convert integer 50 to color :BLACK
4. Convert string 50 to color :BLACK
5. Convert BLACK color to integer :50
6. Convert BLACK color to String :50