Java Enum with Strings

In this guide to Java enum with string values, learn to create enum using strings, iterate over all enum values, get enum value and perform a reverse lookup to find enum by string parameter.

We should always create enum when we have a fixed set of related constants. Enums are inherently singleton so they provide a better performance.

1. Creating Enum with Strings

Java program to create an enum with strings. The given enum contains deployment environments and their respective URLs. URLs are passed to the enum constructor for each enum constant.

public enum Environment 
{
    PROD("https://prod.domain.com:1088/"), 
    SIT("https://sit.domain.com:2019/"), 
    CIT("https://cit.domain.com:8080/"), 
    DEV("https://dev.domain.com:21323/");
 
    private String url;
 
    Environment(String envUrl) {
        this.url = envUrl;
    }
 
    public String getUrl() {
        return url;
    }
}

2. Iterating over Enum Constants

To iterate over enum list, use values() method on enum type which returns all enum constants in an array.

//Get all enums
for(Environment env : Environment.values())
{
    System.out.println(env.name() + " :: " + env.getUrl());
}

Output:

PROD :: https://prod.domain.com:1088/
SIT :: https://sit.domain.com:2019/
CIT :: https://cit.domain.com:8080/
DEV :: https://dev.domain.com:21323/

3. Getting Enum Value

To get a single enum value (e.g., get production URL from enum constant), use the value method that we created.

//Using enum constant reference
String prodUrl = Environment.PROD.getUrl();
 
System.out.println(prodUrl);

Output:

https://prod.domain.com:1088/

4. Getting Enum by Name

If we want to get enum constant using its name, then we should use valueOf() method.

//First get enum constant reference from string
Environment sitUrl = Environment.valueOf("SIT");
 
System.out.println(sitUrl.getUrl());

Output:

https://sit.domain.com:2019/

5. Reverse Lookup – Get Enum Name from Value

We will often have the value of enum with us, and we will need to get the enum name by its value. This is achieved using a reverse lookup.

enum Environment 
{
    PROD("https://prod.domain.com:1088/"), 
    SIT("https://sit.domain.com:2019/"), 
    CIT("https://cit.domain.com:8080/"), 
    DEV("https://dev.domain.com:21323/");
 
    private String url;
 
    Environment(String envUrl) {
        this.url = envUrl;
    }
 
    public String getUrl() {
        return url;
    }
     
    //****** Reverse Lookup ************//
 
    public static Optional<Environment> get(String url) {
        return Arrays.stream(Environment.values())
            .filter(env -> env.url.equals(url))
            .findFirst();
    }
}

To use the reverse lookup in the application code, use enum.get() method.

String url = "https://sit.domain.com:2019/";

Optional<Environment> env = Environment.get(url);
 
System.out.println(env.get());

Output:

SIT

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

9 thoughts on “Java Enum with Strings”

  1. See SO Thread

    Java 8

    public static final Environment  getByValue(String value){
        return Arrays.stream(Environment.values())
                     .filter(enumEnv > enumEnv.url.equals(value))
                     .findFirst().orElse(ENV_UNKNOWN);
    }
    Reply
  2. Also you could use lambda for search enum value by string

    public static Environment get(String url) {
    return Arrays.stream(values())
    .filter(env -> env.url().equals(url))
    .findFirst()
    .orElse(<some default value>)
    // .orElseThrow(() -> new IllegalArgumentException("Invalid spConnectionId: " + url);  --- or go thru exception
    }
    
    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.