Java program to lookup enum by string value



An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). For example, we have constants like week days, months, or set of languages etc. In Java, we can create an enum to represent these constants.

In this article, we will learn how to lookup an enum by its string value in Java.

Java Program to Lookup Enum by String Value

lets learn how you can convert a string to its corresponding enum value using the following ways and also print the enum value:

Using valueOf() Method

The valueOf() method of the Enum class is used for converting a string to its enum value. It takes a string as an argument and returns its enum value if it exists. If the string does not match any enum constant, it throws an IllegalArgumentException.

Steps to use valueOf() method

  • First, we will declare an enum called Languages with constants.
  • After that, we will create a string variable that contains the input value (e.g., "Java").
  • Convert the input string to uppercase using the toUpperCase() method to match the enum constant names, which are typically in uppercase.
  • We will be using the valueOf() method of the Languages enum to match the uppercase string with the enum value.
  • Print the matched enum value.

Example

In the following example, we will create an enum called ProgrammingLanguage and then use the valueOf() method to convert a string to its corresponding enum value.

import java.util.Arrays;
import java.util.List;
public class EnumLookupExample {
   public enum ProgrammingLanguage {
      JAVA, PYTHON, JAVASCRIPT, CPLUSPLUS, SWIFT;
   }

   public static void main(String[] args) {
      String input = "Java";
      try {
         ProgrammingLanguage language = ProgrammingLanguage.valueOf(input.toUpperCase());
         System.out.println("The enum value for the string '" + input + "' is: " + language);
      } catch (IllegalArgumentException e) {
         System.out.println("No enum constant found for the string: " + input);
      }
   }
}

Output

Following is the output of the above code:

The enum value for the string 'Java' is: JAVA

Using EnumSet and stream()

EnumSet is a type of Set that can only be used with enum types. It provides a way to create a set of enum constants. We can use EnumSet along with the stream() method to filter and find the enum constant that matches the given string.

Next, we will use the filter method to check if the enum constant's name matches the given string (ignoring case) and then use findFirst() to get the first matching enum constant.

We will also use Optional to handle the case where no matching enum constant is found.

Example

In the following example, we will use EnumSet and stream() to find the enum constant that matches the given string.

import java.util.EnumSet;
import java.util.Optional;
import java.util.stream.Stream;

public class EnumLookupExample {
   public enum ProgrammingLanguage {
      JAVA, PYTHON, JAVASCRIPT, CPLUSPLUS, SWIFT;
   }

   public static void main(String[] args) {
      String input = "Java";
      Optional<ProgrammingLanguage> language = EnumSet.allOf(ProgrammingLanguage.class)
            .stream()
            .filter(lang -> lang.name().equalsIgnoreCase(input))
            .findFirst();

      if (language.isPresent()) {
         System.out.println("The enum value for the string '" + input + "' is: " + language.get());
      } else {
         System.out.println("No enum constant found for the string: " + input);
      }
   }
}

Following is the output of the above code:

The enum value for the string 'Java' is: JAVA

Using .name() Method

The .name() method of the Enum class allows us to get the name of the enum constant as a string. We can use this method to compare the enum constant's name with the given string.

Steps to use .name() method

  • First, we will declare an enum called Languages with constants.
  • Print a message indicating that you will display all enum values.
  • Use the .name() method to print each enum value in its string form.
  • Display the enum values one by one.

Example

In the following example, we will create an enum called ProgrammingLanguage and then use the .name() method to convert a string to its corresponding enum value.

enum Languages {
   JAVA,
   SCALA,   
   PYTHON,
   MYSQL;
}
public class Demo {
   public static void main(String[] args) {
   System.out.print("The values of the ENUM are: ");
   System.out.print(Languages.JAVA.name() + ", ");
   System.out.print(Languages.SCALA.name() + ", ");
   System.out.print(Languages.PYTHON.name() + ", ");
   System.out.print(Languages.MYSQL.name() + "\n");
   }
}

Following is the output of the above code:

The values of the ENUM are:
JAVA, SCALA, PYTHON, MYSQL
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-09-01T12:56:48+05:30

872 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements