Java 中对于枚举双值的使用

Java 中对于枚举双值的使用_第1张图片

 转成键值对或者是map 的形式;

键值对的类

 

public class Kav {

	/**
	 * 键
	 */
	private String key;

	/**
	 * 值
	 */
	private String value;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}

 

 

 

枚举:

 

public enum AprrovalStatus {
	COMMITED(1L, "待审批"), 
	APPROVALED(2L, "已审批通过"), 
	REJECTED(3L, "已拒绝"), 
	SYNCHRONIZED(4L, "已同步"),
	EXPIRED(5L,"已失效"),
	TEMPORARY(6L,"暂存"),
	REMOVE(7L,"撤回");

	private final Long key;
	private final String value;

	public Long getKey() {
		return key;
	}

	public String getValue() {
		return value;
	}

	AprrovalStatus(Long key, String value) {
		this.key = key;
		this.value = value;
	}

	/**
	 * 根据key获取value
	 * 
	 * @param key
	 *            : 键值key
	 * @return String
	 */
	public static String getValueByKey(String key) {
		AprrovalStatus[] enums = AprrovalStatus.values();
		for (int i = 0; i < enums.length; i++) {
			if (enums[i].getKey().equals(key)) {
				return enums[i].getValue();
			}
		}
		return "";
	}

	/**
	 * 转换为'KAV'列表
	 * 
	 * @return List
	 */
	public static List toKavs() {
		List l_kavs = new ArrayList();
		AprrovalStatus[] enums = AprrovalStatus.values();
		for (int i = 0; i < enums.length; i++) {
			Kav kav = new Kav();
			kav.setKey(enums[i].getKey().toString());
			kav.setValue(enums[i].getValue());
			l_kavs.add(kav);
		}
		return l_kavs;
	}

	/**
	 * 转换为MAP集合
	 * 
	 * @returnMap
	 */
	public static Map toMap() {
		Map map = new HashMap();
		AprrovalStatus[] enums = AprrovalStatus.values();
		for (int i = 0; i < enums.length; i++) {
			map.put(enums[i].getKey().toString(), enums[i].getValue());
		}
		return map;
	}
}

 

对于单只枚举的使用:

public enum MistakeHandleStatusEnum {

	HANDLED("已处理"),

	NOHANDLE("未处理");

	private String desc;

	private MistakeHandleStatusEnum(String desc) {
		this.desc = desc;
	}

	public String getDesc() {
		return desc;
	}

	public static MistakeHandleStatusEnum getEnum(String name) {
		MistakeHandleStatusEnum[] arry = MistakeHandleStatusEnum.values();
		for (int i = 0; i < arry.length; i++) {
			if (arry[i].name().equals(name)) {
				return arry[i];
			}
		}
		return null;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List toList() {
		MistakeHandleStatusEnum[] ary = MistakeHandleStatusEnum.values();
		List list = new ArrayList();
		for (int i = 0; i < ary.length; i++) {
			Map map = new HashMap();
			map.put("desc", ary[i].getDesc());
			map.put("name", ary[i].name());
			list.add(map);
		}
		return list;
	}

}

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据结构与算法)