java tutorial: Java enum check if value exists

Java enum is data types that we often use in java projects to define an exhaustive set of constants. For example, task status definition, file type definition, etc.

This article shares with you how to check whether a given value is in an enumeration in java.

There are four ways to check whether the value is in the enum:

  • Enum class provides inspection methods
  • Check with for loop
  • Use stream iterators
  • Use a tool class similar to Apache EnumUtils

Enum class provides inspection methods

When we define the enum class, we provide the corresponding check method. This method is suitable for us to use our own defined enum class.

For example, the following enum class provides the isExist method to check whether the name exists in the enum.

public enum JobStatus {
    CREATED(1, "CREATED"),
    EXECUTING(2, "EXECUTING"),
    FINISHED(3, "FINISHED"),
    FAILED(0, "FAILED");

    Integer code;
    String name;

    JobStatus( Integer code, String name) {
        this.name = name;
        this.code = code;
    }

    public static Boolean isExist(String name) {
        if(StringUtils.isBlank(name)) {
            return false;
        }

        for(JobStatus item : JobStatus.values()) {
            if(name.equals(item.name)) {
                return true;
            }
        }
        return false;
    }
}

Check with for loop

In the following example, a for loop is used to traverse the JobStatus enum value matching.

public class CheckInEnum {
    public static void main(String[] args) {
        String statusName = "FAILED";
        Boolean aBoolean = checkInEnum(JobStatus.values(), statusName);
        System.out.println("aBoolean: " + aBoolean);
        System.out.println("---------------------");
    }

    /**
     * Check with for loop
     * @param jobStatus
     * @param name
     * @return
     */
    private static Boolean checkInEnum(JobStatus[] jobStatus, String name) {
        if(StringUtils.isBlank(name)) {
            return false;
        }
        for(JobStatus item : jobStatus) {
            if(name.equals(item.name)) {
                return true;
            }
        }
        return false;
    }
}

Use stream iterators

In the following example, the java stream iterator is used to traverse the check.

public class CheckInEnum {
    public static void main(String[] args) {
        String statusName = "FAILED";
        Boolean aBoolean2 = checkInEnum(JobStatus.values(), statusName);
        System.out.println("aBoolean2: " + aBoolean2);
        System.out.println("---------------------");
    }

    /**
     * Use stream iterators
     * @param jobStatuses
     * @param name
     * @return
     */
    private static Boolean checkInEnum(JobStatus[] jobStatuses, String name) {
        if(Objects.isNull(jobStatuses) || StringUtils.isBlank(name)) {
            return false;
        }

        Iterator<JobStatus> iterator = Arrays.stream(jobStatuses).iterator();
        while (iterator.hasNext()) {
            if(name.equals(iterator.next().name)) {
                return true;
            }
        }
        return false;
    }
}

Use a tool class similar to Apache EnumUtils

public class CheckInEnum {
    public static void main(String[] args) {
        String statusName = "FAILED";
      
        Boolean aBoolean1 = checkInEnum(JobStatus.class, statusName);
        System.out.println("aBoolean1: " + aBoolean1);
        System.out.println("---------------------");
    }

    /**
     * Use a tool class similar to Apache EnumUtils
     * @param enumClass
     * @param name
     * @param <E>
     * @return
     */
    private static <E extends Enum<E>> Boolean checkInEnum(Class<E> enumClass, String name) {
        if(StringUtils.isBlank(name)) {
            return false;
        }
        try{
            E e = Enum.valueOf(enumClass, name);
            return true;
        }catch (Exception e ) {
            return false;
        }
    }
}

Add a Comment

Your email address will not be published. Required fields are marked *