Java How to Convert String to Enum Easily
In Java, convert a string to an enum by using
EnumType.valueOf("STRING_VALUE"), where EnumType is your enum name and "STRING_VALUE" matches an enum constant exactly.Examples
InputMONDAY
OutputDay.MONDAY
InputFRIDAY
OutputDay.FRIDAY
InputSUNDAY
OutputDay.SUNDAY
How to Think About It
To convert a string to an enum, think of matching the string exactly to one of the enum's named constants. Java provides a built-in method called
valueOf that takes the string and returns the matching enum constant if it exists, otherwise it throws an exception.Algorithm
1
Get the input string representing the enum name.2
Call the enum's <code>valueOf</code> method with the input string.3
If the string matches an enum constant, return that enum constant.4
If no match is found, handle the error or exception.Code
java
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public class Main { public static void main(String[] args) { String input = "FRIDAY"; Day day = Day.valueOf(input); System.out.println(day); } }
Output
FRIDAY
Dry Run
Let's trace converting the string "FRIDAY" to the enum Day.
1
Input string
input = "FRIDAY"
2
Call valueOf
Day.valueOf("FRIDAY") matches Day.FRIDAY
3
Return enum
day = Day.FRIDAY
| Input String | Enum Returned |
|---|---|
| FRIDAY | Day.FRIDAY |
Why This Works
Step 1: Exact Match Required
The valueOf method requires the string to exactly match an enum constant name, including case.
Step 2: Returns Enum Constant
If matched, valueOf returns the enum constant as a typed enum value.
Step 3: Throws Exception if No Match
If the string does not match, valueOf throws an IllegalArgumentException.
Alternative Approaches
Using a loop to match ignoring case
java
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; public static Day fromString(String s) { for (Day d : Day.values()) { if (d.name().equalsIgnoreCase(s)) { return d; } } throw new IllegalArgumentException("No enum constant " + s); } } public class Main { public static void main(String[] args) { String input = "friday"; Day day = Day.fromString(input); System.out.println(day); } }
Allows case-insensitive matching but requires extra code and looping.
Using Optional with streams (Java 8+)
java
import java.util.Arrays; import java.util.Optional; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; public static Optional<Day> fromString(String s) { return Arrays.stream(Day.values()) .filter(d -> d.name().equalsIgnoreCase(s)) .findFirst(); } } public class Main { public static void main(String[] args) { String input = "friday"; Optional<Day> day = Day.fromString(input); day.ifPresent(System.out::println); } }
Returns Optional to handle missing values gracefully, but more complex.
Complexity: O(n) time, O(1) space
Time Complexity
The valueOf method internally searches enum constants, which is typically O(n) where n is the number of enum constants.
Space Complexity
No extra space is needed besides the input string and returned enum reference, so O(1).
Which Approach is Fastest?
Using valueOf is fastest for exact matches. Alternatives with loops or streams add overhead but allow case-insensitive or safe handling.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Enum.valueOf() | O(n) | O(1) | Exact match, simple use |
| Loop with equalsIgnoreCase | O(n) | O(1) | Case-insensitive matching |
| Streams with Optional | O(n) | O(1) | Safe handling with Optional |
Use
EnumType.valueOf(string) for quick exact string to enum conversion.Forgetting that
valueOf is case-sensitive and throws an exception if the string doesn't match exactly.