In this guide to Java enum with string values, learn to create enum using strings, iterate over all enum values, get enum value and to perform reverse lookup to find enum by string parameter.
1. Create java enum with string values
Java program to create enum with strings. This enum contains deployment environments and their respective URLs. URLs are passed to enum constrctor 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. Iterate enum constants
To iterate over enum list, use values()
method on enum type which return 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. Java enum to String
To get single enum value (e.g. get prod URL from enum constant) use it’s value method, you created.
//Using enum constant reference String prodUrl = Environment.PROD.getUrl(); System.out.println(prodUrl);
Output:
https://prod.domain.com:1088/
4. Get enum by name – enum string parameter
If you want to get enum constant using it’s name, then 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
Many times, we will have the value of enum with us, and we will need to get enum name by string value. This is achieved using reverse lookup.
In reverse lookup, enum internally maintains a map of value-enum as key-value pair. This map is searched, application need to find enum with it’s associated string value.
import java.util.HashMap; import java.util.Map; 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; } //****** Reverse Lookup Implementation************// //Lookup table private static final Map<String, Environment> lookup = new HashMap<>(); //Populate the lookup table on loading time static { for(Environment env : Environment.values()) { lookup.put(env.getUrl(), env); } } //This method can be used for reverse lookup purpose public static Environment get(String url) { return lookup.get(url); } }
To use this reverse lookup, in application code use enum.get()
method.
//Get enum constant by string - Reverse Lookup String url = "https://sit.domain.com:2019/"; Environment env = Environment.get(url); System.out.println(env);
Output:
SIT
Read More: Complete Java Enum Guide
Drop me your questions on Java 8 enum in comments section.
Happy Learning !!
Read More:
Leslie Murphy
See SO Thread
Java 8
Vernet Bien-aime
Thanks for this, You saved me at work today.
Rolando murillo
Extremely helpful reverse lookup approach. THANK YOU 😉
Tugce
Thank you.. very clear. great explanation. Thank you so much
Saurabh
This is the first time I understood enum and able to use it easily thanks.
Lukas Vondracek
It is good standard to use get / set methods. So I think getUrl() would be better.
Lokesh Gupta
You are correct. Thanks for the feedback.
Cyril
Also you could use lambda for search enum value by string
Lokesh Gupta
Perfect !! Thanks for sharing !!