如何使用id检索枚举名称?

我的enum为:

 public enum EnumStatus { PASSED(40L, "Has Passed"), AVERAGE(60L, "Has Average Marks"), GOOD(80L, "Has Good Marks"); private java.lang.String name; private java.lang.Long id; EnumStatus(Long id, java.lang.String name) { this.name = name; this.id = id; } public java.lang.String getName() { return name; } public java.lang.Long getId() { return id; } } 

我必须使用id(40,60,80)获得Enum名称( PASSEDAVERAGEGOOD )。 我该怎么做?

在你的enum创建一个静态方法,用于搜索values (隐式方法/成员,不知道它究竟是哪个)并返回相应的值。 对于方法无法找到匹配值的情况,您应该创建一个特殊条目,例如UNKNOWN ,您可以返回该条目。 这样,您不必返回null ,这总是一个坏主意。

 public static EnumStatus getById(Long id) { for(EnumStatus e : values()) { if(e.id.equals(id)) return e; } return UNKNOWN; } 

顺便说一下 – 你的代码似乎错了。 GOOD之后的支架似乎不属于那里。

这可以使用静态映射和静态初始化程序来完成:

 public enum EnumStatus { PASSED(40L, "Has Passed"), AVERAGE(60L, "Has Average Marks"), GOOD(80L, "Has Good Marks"); private static final Map byId = new HashMap(); static { for (EnumStatus e : EnumStatus.values()) { if (byId.put(e.getId(), e) != null) { throw new IllegalArgumentException("duplicate id: " + e.getId()); } } } public static EnumStatus getById(Long id) { return byId.get(id); } // original code follows private java.lang.String name; private java.lang.Long id; EnumStatus(Long id, java.lang.String name) { this.name = name; this.id = id; } public java.lang.String getName() { return name; } public java.lang.Long getId() { return id; } } 

这将给出一个O(1) getById()方法,并将自动检测你是否意外地在枚举中有重复的id。

你使这项工作如下:

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

在您的Enum添加一个方法,并通过传递ID来获取它。

  public static ArrayList getEnumStatusById(ArrayList idList) { ArrayList listById = new ArrayList(); for(EnumStatus es: EnumStatus.values()) { if( idList.contains(es.getId())) { listById.add(es); } } return listById; } 
 public static EnumStatus getById(long id) { for (EnumStatus e : EnumStatus.values()) { if (id == e.getId()) return e; } throw new IllegalArgumentException("oh no"); } 

迭代所有值并比较Id

 for (EnumStatus enumStatus : EnumStatus.values()) { if (..) {..} } 

有时enum的序数与这种id有明确的关系,能够以一种巧妙的方式在这些方法中获得O(1)。 在您的代码中,很明显

EnumStatus.X = 40 + 20 * ordinal

所以你可以利用引擎盖下生成的静态数组 。

 public static EnumStatus fromId(Long id) { int index = (id - 40L) / 20L; return values()[index]; } 

定义合同

 /** * Contract that will allow Types with id to have generic implementation. */ public interface IdentifierType { T getId(); } 

申请合同

 public enum EntityType implements IdentifierType { ENTITY1(1, "ONE), ENTITY2(2, "TWO"); private Integer id; private String name; private EntityType(int id, String name) { this.id = id; this.name = name; } public static EntityType valueOf(Integer id) { return EnumHelper.INSTANCE.valueOf(id, EntityType.values()); } @Override public Integer getId() { return id; } } 

助手/的Util

 public enum EnumHelper { INSTANCE; /** * This will return {@link Enum} constant out of provided {@link Enum} values with the specified id. * @param id the id of the constant to return. * @param values the {@link Enum} constants of specified type. * @return the {@link Enum} constant. */ public , S> T valueOf(S id, T[] values) { if (!values[0].getClass().isEnum()) { throw new IllegalArgumentException("Values provided to scan is not an Enum"); } T type = null; for (int i = 0; i < values.length && type == null; i++) { if (values[i].getId().equals(id)) { type = values[i]; } } return type; } }