如何在Java中使用key int和value作为字符串构建枚举
我有这样的数据 11种作物如何在Java中使用key int和value作为字符串构建枚举,java,enums,Java,Enums,我有这样的数据 11种作物 12种植园 13畜牧业 14渔业 我曾尝试构建如下枚举类: public enum SubSectorEnum { Crops(11), Plantation(12); } 我想要的是,如果我有数据11或12,我想把它转换成农作物或种植园。我的问题是,数据存储在一个数据库中,我只有11或12作为int。我需要的是将int 11和12转换为作物或种植园。如何转换它?如果我了解您需要什么,我想这是一个带有键值的枚举: enum SubSectorEnu
12种植园
13畜牧业
14渔业 我曾尝试构建如下枚举类:
public enum SubSectorEnum {
Crops(11),
Plantation(12);
}
我想要的是,如果我有数据11或12,我想把它转换成农作物或种植园。我的问题是,数据存储在一个数据库中,我只有11或12作为int。我需要的是将int 11和12转换为作物或种植园。如何转换它?如果我了解您需要什么,我想这是一个带有键值的枚举:
enum SubSectorEnum {
Crops(11, "Crops"), Plantation(2, "Plantation");
private final int key;
private final String value;
SubSectorEnum(int key, String value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
}
因此,在获得值或键后:
SubSectorEnum.Crops.getValue();
SubSectorEnum.Crops.getKey();
如果我了解您需要什么,我想这是一个带有键值的枚举:
enum SubSectorEnum {
Crops(11, "Crops"), Plantation(2, "Plantation");
private final int key;
private final String value;
SubSectorEnum(int key, String value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
}
因此,在获得值或键后:
SubSectorEnum.Crops.getValue();
SubSectorEnum.Crops.getKey();
根据您想要使用的逻辑,您可以使用两种简单的方法 我建议使用第二个
public enum SubSectorEnum {
Crops(11), Plantation(12);
public int id;
SubSectorEnum(int id) {
this.id = id;
}
public int getId() {
return id;
}
// 1 approach using switch
public SubSectorEnum convertIdToEnum(int id) {
SubSectorEnum subSectorEnum = Crops; // Default
switch (id) {
case 11:
subSectorEnum = Crops;
break;
case 12:
subSectorEnum = Plantation;
break;
default:
break;
}
return subSectorEnum;
}
// 2 approach using a map
private static final Map<Integer, SubSectorEnum> map = new HashMap<Integer, SubSectorEnum>();
static {
for (SubSectorEnum subSectorEnum : SubSectorEnum.values())
map.put(subSectorEnum.getId(), subSectorEnum);
}
public static SubSectorEnum getEnumFromMap(int id) {
return map.get(id);
}
}
根据您想要使用的逻辑,您可以使用两种简单的方法 我建议使用第二个
public enum SubSectorEnum {
Crops(11), Plantation(12);
public int id;
SubSectorEnum(int id) {
this.id = id;
}
public int getId() {
return id;
}
// 1 approach using switch
public SubSectorEnum convertIdToEnum(int id) {
SubSectorEnum subSectorEnum = Crops; // Default
switch (id) {
case 11:
subSectorEnum = Crops;
break;
case 12:
subSectorEnum = Plantation;
break;
default:
break;
}
return subSectorEnum;
}
// 2 approach using a map
private static final Map<Integer, SubSectorEnum> map = new HashMap<Integer, SubSectorEnum>();
static {
for (SubSectorEnum subSectorEnum : SubSectorEnum.values())
map.put(subSectorEnum.getId(), subSectorEnum);
}
public static SubSectorEnum getEnumFromMap(int id) {
return map.get(id);
}
}
如果以后添加了更多枚举值,则可以使用一个简单的查找来遍历
values()
列表以找到合适的匹配项:
public static SubSectorEnum findById(int id) {
for (SubSectorEnum sse : values())
if(id == sse.getId())
return sse;
throw new IllegalArgumentException("No SubSectorEnum with id "+id);
}
如果以后添加了更多枚举值,则可以使用一个简单的查找来遍历
values()
列表以找到合适的匹配项:
public static SubSectorEnum findById(int id) {
for (SubSectorEnum sse : values())
if(id == sse.getId())
return sse;
throw new IllegalArgumentException("No SubSectorEnum with id "+id);
}
看看这个看看这个