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 !!
See SO Thread
Java 8
Thanks for this, You saved me at work today.
Extremely helpful reverse lookup approach. THANK YOU 😉
Thank you.. very clear. great explanation. Thank you so much
This is the first time I understood enum and able to use it easily thanks.
It is good standard to use get / set methods. So I think getUrl() would be better.
You are correct. Thanks for the feedback.
Also you could use lambda for search enum value by string
Perfect !! Thanks for sharing !!