枚舉enum,通過key獲取value,通過value獲取key


public enum DEPT_TJBJ1 {
KV1("k1", "v1"), KV2("k2", "v2"), KV3("k3", "v3"), KV4("k4", "v4");
// 成員變量
private String key;
private String value;
// 構造方法
private DEPT_TJBJ1(String key, String value) {
this.key = key;
this.value = value;
}
// get set 方法
public String getKey() {
return key;
}
public String getValue() {
return value;
}

// 通過key獲取value
public static String getValue(String key) {
DEPT_TJBJ1[] tjbjs = DEPT_TJBJ1.values();
for (DEPT_TJBJ1 tjbj : tjbjs) {
if (tjbj.getKey() .equals(key)) {
return tjbj.value;
}
}
return "";
}

// 通過value獲取key
public static String getKey(String value) {
DEPT_TJBJ1[] tjbjs = DEPT_TJBJ1.values();
for (DEPT_TJBJ1 tjbj : tjbjs) {
if (tjbj.getValue().equals(value)) {
return tjbj.getKey();
}
}
return null;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM