Introduction

Enum was introduced in Java 1.5 as a new type whose fields consist of a fixed set of constants.

An enum type is a special data type that enables a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

Common examples include Days of Week (values of SUNDAY,MONDAY,TUESDAY) .

Table of Content :



When working with java programming, Many times we need to check if an Enum contains a given String value. There are multiple ways to validate this.

1). EnumUtils class of Apache Commons Lang3 library
2). Create custom method
3). Using Java 8's streams API

We will see them one by one.

enum Day { SUNDAY("Sunday"), MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"); private String dayOfWeek; Day(String dayOfWeek) { this.setDayOfWeek(dayOfWeek); } public String getDayOfWeek() { return dayOfWeek; } public void setDayOfWeek(String dayOfWeek) { this.dayOfWeek = dayOfWeek; } }


1). EnumUtils class of Apache Commons Lang

The EnumUtils class has a method called isValidEnum whichChecks if the specified name is a valid enum for the class. It returns a boolean true if the String is contained in the Enum class, and a boolean false otherwise.

EnumUtils.isValidEnum(Day.class, "MONDAY")

This method differs from Enum.valueOf(java.lang.Class, java.lang.String) in that checks if the name is a valid enum without needing to catch the exception.

The following code example demonstrates the usage of EnumUtils.isValidEnum()

package com.javacodestuffs.core.enums; import org.apache.commons.lang3.EnumUtils; public class FindInEnumUsingLang3 { public static void main(String[] args) { // Is SATURDAY("Saturday") a part of the enum? System.out.println("Day Enum contains SUNDAY : " + EnumUtils.isValidEnum(Day.class, "SATURDAY")); } } output: Day Enum contains SUNDAY : true

2). Using Custom Method

Create custom method as given below then you call isInEnum("THURSDAY", Day.class).

public static <E extends Enum<E>> boolean isInEnum(String value, Class<E> enumClass) { for (E e : enumClass.getEnumConstants()) { if(e.name().equals(value)) { return true; } } return false; }

FindInEnumUsingCustomMethod.java

package com.javacodestuffs.core.enums; public class FindInEnumUsingCustomMethod { public static void main(String[] args) { // Is SATURDAY("THURSDAY") a part of the enum? System.out.println("Day Enum contains THURSDAY : " + isInEnum("THURSDAY", Day.class)); } public static <E extends Enum <E>> boolean isInEnum(String value, Class <E> enumClass) { for (E e: enumClass.getEnumConstants()) { if (e.name().equals(value)) { return true; } } return false; } } output: Day Enum contains THURSDAY : true


3). Using Java 8's streams API

In this case enumns are converted to array, anyMatch and equals method's are used.You can also put this method directly into the Enum class, and remove one of the parameters.

public boolean isInEnum(String value, Class<E> enumClass) { return Arrays.stream(enumClass.getEnumConstants()).anyMatch(e -> e.name().equals(value)); }

FindInEnumUsingStream.java

import java.util.Arrays; public class FindInEnumUsingStream { public static void main(String[] args) { System.out.println("Day Enum contains SUNDAY : "+isInEnum("SUNDAY", Day.class)); } public static boolean isInEnum(String dayOfWeek, Class <Day> enumClass) { return Arrays.stream(enumClass.getEnumConstants()).anyMatch(e - >e.name().equals(dayOfWeek)); } } output: Day Enum contains SUNDAY : true

Post/Questions related to How to check if an enum contains String

Enum in Java
How to check enum contains value in java?
How to get value from enum in java?
How to convert enum to list in java?
How to get enum key from value in java?
How to pass enum as parameter in java?
How to use enum constructor in java?
How to use enum in switch case java?
How to override valueof in enum java?
What is the difference between iterator and enumeration in java?
How to get all enum values in java?

In this article, we have seen the   How to check if an enum contains String in Java with examples. All source code in the article can be found in the GitHub repository.