Java Program to Lookup enum by String value

Created with Sketch.

Java Program: Looking Up Enums by String Value

Introduction

In Java, enums provide a way to represent a fixed set of constants. Enum constants are typically defined using uppercase letters, but there might be situations where you need to look up an enum based on its associated string value. This blog post presents a Java program that demonstrates how to perform a reverse lookup of enums by string values. We will explore the algorithm, provide practical examples, and explain the step-by-step process to achieve this functionality.

Algorithm Overview

The process of looking up enums by string values involves the following key steps:

  1. Create an Enum: Define an enum with constants and associated string values.

  2. Implement a Reverse Lookup Map: Create a map where the keys are string values, and the values are the corresponding enum constants.

  3. Perform the Lookup: Use the reverse lookup map to find the enum based on a given string value.

Now, let’s delve into the Java program, breaking down the algorithm with detailed explanations and examples.

Java Program: Looking Up Enums by String Value

import java.util.HashMap;
import java.util.Map;

public class EnumLookupExample {

    // Step 1: Create an Enum
    enum Color {
        RED("FF0000"),
        GREEN("00FF00"),
        BLUE("0000FF");

        private final String hexCode;

        Color(String hexCode) {
            this.hexCode = hexCode;
        }

        public String getHexCode() {
            return hexCode;
        }
    }

    // Step 2: Implement a Reverse Lookup Map
    private static final Map<String, Color> reverseLookupMap = new HashMap<>();

    static {
        for (Color color : Color.values()) {
            reverseLookupMap.put(color.getHexCode(), color);
        }
    }

    public static Color getColorByHexCode(String hexCode) {
        // Step 3: Perform the Lookup
        return reverseLookupMap.get(hexCode);
    }

    public static void main(String[] args) {
        // Example: Lookup enum by string value
        String hexCodeToLookup = "00FF00";
        Color foundColor = getColorByHexCode(hexCodeToLookup);

        if (foundColor != null) {
            System.out.println("Color for hex code " + hexCodeToLookup + ": " + foundColor);
        } else {
            System.out.println("Color not found for hex code " + hexCodeToLookup);
        }
    }
}

In this example, we have an enum Color representing three colors with their associated hex codes. The goal is to perform a reverse lookup to find the enum constant based on a given hex code.

Output Example

Color for hex code 00FF00: GREEN

Explanation of the Algorithm

Let’s break down the algorithm step by step:

Step 1: Create an Enum

We define an enum Color with constants representing colors and their associated hex codes.

enum Color {
    RED("FF0000"),
    GREEN("00FF00"),
    BLUE("0000FF");

    private final String hexCode;

    Color(String hexCode) {
        this.hexCode = hexCode;
    }

    public String getHexCode() {
        return hexCode;
    }
}

Step 2: Implement a Reverse Lookup Map

We create a map (reverseLookupMap) where the keys are string values (hex codes), and the values are the corresponding enum constants. This map is populated during the static initialization block.

private static final Map<String, Color> reverseLookupMap = new HashMap<>();

static {
    for (Color color : Color.values()) {
        reverseLookupMap.put(color.getHexCode(), color);
    }
}

Step 3: Perform the Lookup

We implement a method getColorByHexCode that performs the reverse lookup by using the hex code as the key in the map.

public static Color getColorByHexCode(String hexCode) {
    return reverseLookupMap.get(hexCode);
}

Conclusion

Looking up enums by string values is a practical technique in scenarios where you have string representations of enum constants. By implementing a reverse lookup map, you can efficiently retrieve enum constants based on their associated string values. This capability is valuable for applications that involve external configurations, user inputs, or data interchange with string representations.

As a Java developer, understanding how to perform reverse lookups for enums enhances your ability to work with enumerated types in diverse scenarios. Incorporate these techniques into your projects, explore additional enum functionalities, and deepen your expertise in leveraging enums for robust and maintainable code. The ability to perform lookups in both directions adds flexibility and usability to your enum-based implementations.

Leave a Reply

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