发布于 

java,字符串转枚举

在枚举中新增一个静态函数,枚举全部如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public enum bl_type {
USER_ID("user_id"),
ACCOUNT_ID("account_id"),
ADDRESS("address");
private String value;

bl_type(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public static bl_type valueOfName(String name) {
name=name.toLowerCase();//转为小写
Map<String, bl_type> MAP = new HashMap<>();
for (bl_type season : values()) {
MAP.put(season.value, season);
}
bl_type em=MAP.get(name);
if(em==null)
em=MAP.get(name.toUpperCase());//转为大写
return MAP.get(name);
}

}

调用:

1
bl_type.valueOfName(rs.getString("type"))