Java获取枚举值给定Enum值

时间:2023-01-26 14:10:21

How can I get the name of a Java Enum type given its value?

如何获取Java Enum类型的名称?

I have the following code which works for a particular Enum type, can I make it more generic?

我有以下代码适用于特定的枚举类型,我可以使它更通用吗?

public enum Category {

    APPLE("3"), 
    ORANGE("1"), 

    private final String identifier;

    private Category(String identifier) {
        this.identifier = identifier;
    }

    public String toString() {
        return identifier;
    }

    public static String getEnumNameForValue(Object value){
        Category[] values = Category.values();
        String enumValue = null;
        for(Category eachValue : values) {
            enumValue = eachValue.toString();

            if (enumValue.equalsIgnoreCase(value)) {
                return eachValue.name();
            }
        }
        return enumValue;
    }
}

4 个解决方案

#1


36  

You should replace your getEnumNameForValue by a call to the name() method.

您应该通过调用name()方法替换您的getEnumNameForValue。

#2


22  

Try below code

试试下面的代码

public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
        name=stringVal;
    }
    public String toString(){
        return name;
    }

    public static String getEnumByString(String code){
        for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
            if(code == e.name) return e.name();
        }
        return null;
    }
}

Now you can use below code to retrieve the Enum by Value

现在,您可以使用下面的代码来按值检索枚举

SalaryHeadMasterEnum.getEnumByString("Basic Pay")

Use Below code to get ENUM as String

使用下面的代码将ENUM作为字符串

SalaryHeadMasterEnum.BASIC_PAY.name()

Use below code to get string Value for enum

使用下面的代码来获取枚举的字符串值

SalaryHeadMasterEnum.BASIC_PAY.toString()

#3


4  

Try, the following code..

试试,以下代码..

    @Override
    public String toString() {
    return this.name();
    }

#4


1  

Here is the below code, it will return the Enum name from Enum value.

以下是代码,它将从枚举值返回枚举名称。

public enum Test {

    PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
            "DivideByZero");
    private String operationName;

    private Test(final String operationName) {
        setOperationName(operationName);
    }

    public String getOperationName() {
        return operationName;
    }

    public void setOperationName(final String operationName) {
        this.operationName = operationName;
    }

    public static Test getOperationName(final String operationName) {

        for (Test oprname : Test.values()) {
            if (operationName.equals(oprname.toString())) {
                return oprname;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return operationName;
    }
}

public class Main {
    public static void main(String[] args) {

        Test test = Test.getOperationName("Plus One");
        switch (test) {
        case PLUS:
            System.out.println("Plus.....");
            break;
        case MINUS:
            System.out.println("Minus.....");
            break;

        default:
            System.out.println("Nothing..");
            break;
        }
    }
}

#1


36  

You should replace your getEnumNameForValue by a call to the name() method.

您应该通过调用name()方法替换您的getEnumNameForValue。

#2


22  

Try below code

试试下面的代码

public enum SalaryHeadMasterEnum {

    BASIC_PAY("basic pay"),
    MEDICAL_ALLOWANCE("Medical Allowance");

    private String name;

    private SalaryHeadMasterEnum(String stringVal) {
        name=stringVal;
    }
    public String toString(){
        return name;
    }

    public static String getEnumByString(String code){
        for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
            if(code == e.name) return e.name();
        }
        return null;
    }
}

Now you can use below code to retrieve the Enum by Value

现在,您可以使用下面的代码来按值检索枚举

SalaryHeadMasterEnum.getEnumByString("Basic Pay")

Use Below code to get ENUM as String

使用下面的代码将ENUM作为字符串

SalaryHeadMasterEnum.BASIC_PAY.name()

Use below code to get string Value for enum

使用下面的代码来获取枚举的字符串值

SalaryHeadMasterEnum.BASIC_PAY.toString()

#3


4  

Try, the following code..

试试,以下代码..

    @Override
    public String toString() {
    return this.name();
    }

#4


1  

Here is the below code, it will return the Enum name from Enum value.

以下是代码,它将从枚举值返回枚举名称。

public enum Test {

    PLUS("Plus One"), MINUS("MinusTwo"), TIMES("MultiplyByFour"), DIVIDE(
            "DivideByZero");
    private String operationName;

    private Test(final String operationName) {
        setOperationName(operationName);
    }

    public String getOperationName() {
        return operationName;
    }

    public void setOperationName(final String operationName) {
        this.operationName = operationName;
    }

    public static Test getOperationName(final String operationName) {

        for (Test oprname : Test.values()) {
            if (operationName.equals(oprname.toString())) {
                return oprname;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return operationName;
    }
}

public class Main {
    public static void main(String[] args) {

        Test test = Test.getOperationName("Plus One");
        switch (test) {
        case PLUS:
            System.out.println("Plus.....");
            break;
        case MINUS:
            System.out.println("Minus.....");
            break;

        default:
            System.out.println("Nothing..");
            break;
        }
    }
}