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 an 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 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 getUrl() method that we created.

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.

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

Comments

Subscribe
Notify of
guest
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode