Java Enum with Multiple Values

Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it.

1. How to Create Enum with Multiple Values

The syntax to create an enum with multiple values is very similar to the syntax of enum with a single value assigned to it. we should do the following steps to have an enum with different values:

  • Create enum constructor which accepts multiple values
  • Assign each constructor argument to a member field in the enum definition
  • Create getter methods so we can access any of the values assigned to a particular enum constant
  • Create a reverse lookup method so we can get the enum constant from any given enum value assigned to it

2. Example of Enum with Multiple Values

In the given example, we are creating an enum AccountStatus. In our application, we can denote an account’s status with any of the multiple values.

For instance, we can indicate an active status with the strings “A“, “active" and even an integer value 1. Similarly, other statuses can have multiple values assigned to them.

Here is the sourcecode for such an enum.

public enum AccountStatus 
{
    PURGED("P", "purged", -1),
    INACTIVE("I", "inactive", 0),
    ACTIVE("A", "active", 1);

    private final String shortName;
    private final String fullName;
    private final int code;

    AccountStatus(String shortName, String fullName, int code) {
        this.shortName = shortName;
        this.fullName = fullName;
        this.code = code;
    }

    public String getShortName() {
        return shortName;
    }

    public String getFullName() {
        return fullName;
    }

    public int getCode() {
        return code;
    }

    // Reverse lookup methods
    public static Optional<AccountStatus> getAccountStatusByValue(String value) {
        return Arrays.stream(AccountStatus.values())
            .filter(accStatus -> accStatus.shortName.equals(value) 
                || accStatus.fullName.equals(value))
            .findFirst();
    }
    
    public static Optional<AccountStatus> getAccountStatusByValue(int value) {
        return Arrays.stream(AccountStatus.values())
            .filter(accStatus -> accStatus.code == value)
            .findFirst();
    }
}

Let’s see how to use this enum in our application. We will first list down all the statuses available in the application. Then we will apply reverse lookup to see what enum constant is associated with value 0 or string “A“.

import java.util.Arrays;
import java.util.Optional;

public class EnumWithMultipleValues 
{
    public static void main(String[] args) 
    {
        //Print all enum and values
        
        for(AccountStatus as : AccountStatus.values()) {
            System.out.println("Status " + as.getCode() + " is : " + as.getFullName());
        }
        
        //Reverse Lookup Examples
        
        Optional<AccountStatus> statusEnum 
            = AccountStatus.getAccountStatusByValue(0);
        
        if(statusEnum.isPresent()) {
            System.out.println("Account Status Full Name: " + statusEnum.get().getFullName());
            System.out.println("Account Status Short name: " + statusEnum.get().getShortName());
        }
        
        Optional<AccountStatus> activeStatusEnum
            = AccountStatus.getAccountStatusByValue("A");
        
        if(statusEnum.isPresent()) {
            System.out.println("Account Status Full Name : " + activeStatusEnum.get().getFullName());
            System.out.println("Account Status Code : " + activeStatusEnum.get().getCode());
        }
    }
}

Program output.

Status -1 is : purged
Status 0 is : inactive
Status 1 is : active

Account Status Full Name: inactive
Account Status Short name: I

Account Status Full Name : active
Account Status Code : 1

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.