使用存儲在Enum中做爲字符串文字的值的最佳方法是什麼? 例如: java
public enum Modes { some-really-long-string, mode1, mode2, mode3 }
而後,稍後我能夠使用Mode.mode1
將其字符串表示形式返回爲mode1
。 無需繼續調用Mode.mode1.toString()
。 ide
您能夠爲每一個枚舉值覆蓋toString()
方法。 this
例: spa
public enum Country { DE { @Override public String toString() { return "Germany"; } }, IT { @Override public String toString() { return "Italy"; } }, US { @Override public String toString() { return "United States"; } } }
用法: code
public static void main(String[] args) { System.out.println(Country.DE); // Germany System.out.println(Country.IT); // Italy System.out.println(Country.US); // United States }
正如Benny Neugebauer所提到的,您能夠覆蓋toString()。 可是,代替覆蓋每一個枚舉字段的toString,我更喜歡這樣的事情: ip
public enum Country{ SPAIN("España"), ITALY("Italia"), PORTUGAL("Portugal"); private String value; Country(final String value) { this.value = value; } public String getValue() { return value; } @Override public String toString() { return this.getValue(); } }
您還能夠添加一個靜態方法來檢索全部字段,打印全部字段等。只需調用getValue便可獲取與每一個Enum項關聯的字符串。 字符串
您能夠簡單地使用: get
""+ Modes.mode1
我爲您解決的問題! string
import java.util.HashMap; import java.util.Map; public enum MapEnumSample { Mustang("One of the fastest cars in the world!"), Mercedes("One of the most beautiful cars in the world!"), Ferrari("Ferrari or Mercedes, which one is the best?"); private final String description; private static Map<String, String> enumMap; private MapEnumSample(String description) { this.description = description; } public String getEnumValue() { return description; } public static String getEnumKey(String name) { if (enumMap == null) { initializeMap(); } return enumMap.get(name); } private static Map<String, String> initializeMap() { enumMap = new HashMap<String, String>(); for (MapEnumSample access : MapEnumSample.values()) { enumMap.put(access.getEnumValue(), access.toString()); } return enumMap; } public static void main(String[] args) { // getting value from Description System.out.println(MapEnumSample.getEnumKey("One of the fastest cars in the world!")); // getting value from Constant System.out.println(MapEnumSample.Mustang.getEnumValue()); System.out.println(MapEnumSample.getEnumKey("One of the most beautiful cars in the world!")); System.out.println(MapEnumSample.Mercedes.getEnumValue()); // doesnt exist in Enum System.out.println("Mustang or Mercedes, which one is the best?"); System.out.println(MapEnumSample.getEnumKey("Mustang or Mercedes, which one is the best?") == null ? "I don't know!" : "I believe that " + MapEnumSample.getEnumKey("Ferrari or Mustang, which one is the best?") + " is the best!."); // exists in Enum System.out.println("Ferrari or Mercedes, wich one is the best?"); System.out.println(MapEnumSample.getEnumKey("Ferrari or Mercedes, which one is the best?") == null ? "I don't know!" : "I believe that " + MapEnumSample.getEnumKey("Ferrari or Mercedes, which one is the best?") + " is the best!"); } }
package com.common.test; public enum Days { monday(1,"Monday"),tuesday(2,"Tuesday"),wednesday(3,"Wednesday"), thrusday(4,"Thrusday"),friday(5,"Friday"),saturday(6,"Saturday"),sunday(7,"Sunday"); private int id; private String desc; Days(int id,String desc){ this.id=id; this.desc=desc; } public static String getDay(int id){ for (Days day : Days.values()) { if (day.getId() == id) { return day.getDesc(); } } return null; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } };