Java getting the Enum name given the Enum Value

前端 未结 5 492
轻奢々
轻奢々 2020-12-15 14:55

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

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

相关标签:
5条回答
  • 2020-12-15 15:26

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

    0 讨论(0)
  • 2020-12-15 15:29

    Try, the following code..

        @Override
        public String toString() {
        return this.name();
        }
    
    0 讨论(0)
  • 2020-12-15 15:30

    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(e.name.equals(code)) 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

    SalaryHeadMasterEnum.BASIC_PAY.name()
    

    Use below code to get string Value for enum

    SalaryHeadMasterEnum.BASIC_PAY.toString()
    
    0 讨论(0)
  • 2020-12-15 15:34

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 15:34

    In such cases, you can convert the values of enum to a List and stream through it. Something like below examples. I would recommend using filter().

    Using ForEach:

    List<Category> category = Arrays.asList(Category.values());
    category.stream().forEach(eachCategory -> {
                if(eachCategory.toString().equals("3")){
                    String name = eachCategory.name();
                }
            });
    

    Or, using Filter:

    When you want to find with code:

    List<Category> categoryList = Arrays.asList(Category.values());
    Category category = categoryList.stream().filter(eachCategory -> eachCategory.toString().equals("3")).findAny().orElse(null);
    
    System.out.println(category.toString() + " " + category.name());
    

    When you want to find with name:

    List<Category> categoryList = Arrays.asList(Category.values());
    Category category = categoryList.stream().filter(eachCategory -> eachCategory.name().equals("Apple")).findAny().orElse(null);
    
    System.out.println(category.toString() + " " + category.name());
    

    Hope it helps! I know this is a very old post, but someone can get help.

    0 讨论(0)
提交回复
热议问题