How to retrieve Enum name using the id?

前端 未结 11 1700
一整个雨季
一整个雨季 2020-12-05 13:11

I have the enum as:

public enum EnumStatus {

    PASSED(40L, \"Has Passed\"),
    AVERAGE(60L, \"Has Average Marks\"),
    GOOD(80L, \"Has Good         


        
11条回答
  •  囚心锁ツ
    2020-12-05 14:09

    You make this work as follows:

    public static String fromId(long id) {
            for (EnumStatus es : EnumStatus.values()) {
                if (es.id.equals(id)) {
                    return es.getName();
                }
            }
            throw new IllegalArgumentException();
    }
    

提交回复
热议问题