Java: check if an enum contains a given string
Codemia Knowledge Hub ·
Use Arrays.stream(MyEnum.values()).anyMatch(e -> e.name().equals(input)). Unlike Enum.valueOf(), it does not throw IllegalArgumentException for unknown values. For hot paths, build a static Set<String> or Map inside the enum once. The sections below also cover case-insensitive matching and matching on custom enum fields.
All examples use this enum:
Stream with anyMatch
The cleanest option for a simple existence check:
Null-safe version
valueOf with exception handling
Enum.valueOf() returns the constant if it exists and throws IllegalArgumentException if it does not:
It works, but using exceptions for control flow is a code smell, and building the stack trace has a measurable cost when invalid values are common.
Pre-built Set for fast lookups
If you check membership often (for example when validating API requests), build a Set once and reuse it:
Lookups are O(1) instead of O(n), which matters for enums with many constants. A set from Collectors.toUnmodifiableSet() does not permit nulls, so its contains(null) throws NullPointerException. Add a null check before the lookup if the input can be null.
Case-insensitive matching
API input often arrives in lowercase or mixed case.
Uppercase the input
Stream with equalsIgnoreCase
Pre-built case-insensitive map
This is the fastest option:
Matching on custom fields
Enums often carry display names, database codes or API values:
Usage:
Comparing the approaches
| Method | Time Complexity | Handles null | Case-insensitive | Returns Enum Value |
| Stream + anyMatch | O(n) | Must handle manually | With equalsIgnoreCase | No |
| valueOf + try/catch | O(1) amortized | Yes (catches NPE) | With toUpperCase | Yes |
| Pre-built Set | O(1) | No (unmodifiable set throws NPE) | With lowercase keys | No |
| Pre-built Map | O(1) | Yes (returns null) | With lowercase keys | Yes |
Generic utility for any enum
To check membership across several enums, write a generic helper:
Apache Commons Lang EnumUtils
If the project already depends on Apache Commons Lang:
Mistakes to avoid
- Using valueOf for validation. It throws
IllegalArgumentExceptionfor missing values. Exceptions for expected cases, such as validating user input, are inefficient and make debugging noisy. Use a stream or set-based check. - Forgetting that name() is case-sensitive.
Status.valueOf("active")throws because enum names are uppercase by convention. Normalize case for user input, API payloads and config files. - Calling values() in a loop.
values()creates a new array on every call, which adds GC pressure in hot loops. Cache it or use a pre-built Set or Map. - Null handling.
Enum.valueOf(null)throwsNullPointerException, notIllegalArgumentException. Null-check input before passing it to enum methods. - Assuming enum names match database or API values. Constants are usually UPPER_SNAKE_CASE, while your API may use camelCase or lowercase. Use a custom field and lookup map for external values instead of
name().
Related reading
- Java Class that implements Map and keeps insertion order?
- java Class.isInstance vs Class.isAssignableFrom
- Java code optimization leads to numerical inaccuracies and errors
- java codility Max-Counters
- java codility training Genomic-range-query
- Java concurrency Countdown latch vs Cyclic barrier
- Java ConcurrentHashMap actions atomicity
- Java Constructor Inheritance
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.