使用枚举作为地图的关键 [英] Using enum as key for map
本文介绍了使用枚举作为地图的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用枚举
作为键和一个对象作为值。以下是示例代码片段:
I want to use enum
as keys and a object as value. Here is the sample code snippet:
public class DistributorAuditSection implements Comparable<DistributorAuditSection>{
private Map questionComponentsMap;
public Map getQuestionComponentsMap(){
return questionComponentsMap;
}
public void setQuestionComponentsMap(Integer key, Object questionComponents){
if((questionComponentsMap == null) || (questionComponentsMap != null && questionComponentsMap.isEmpty())){
this.questionComponentsMap = new HashMap<Integer,Object>();
}
this.questionComponentsMap.put(key,questionComponents);
}
}
现在是一个正常的具有整数键的哈希表,对象作为值。现在我想将其更改为 Enummap
。所以我可以使用枚举
键。我也不知道如何使用 Enummap
检索该值。
It is now a normal hashmap with integer keys and and object as values. Now I want to change it to Enummap
. So that I can use the enum
keys. I also don't know how to retrieve the value using Enummap
.
推荐答案
p>与相同的原则Map
只需声明枚举,并将其用作EnumMap的键。
It the same principle as Map
Just Declare enum and use it as Key to EnumMap.
public enum Colors {
RED, YELLOW, GREEN
}
Map<Colors, String> enumMap = new EnumMap<Colors, String>(Colors.class);
enumMap.put(Colors.RED, "red");
String value = enumMap.get(Colors.RED);
您可以找到更多关于枚举
这里
You can find more about Enums
here
这篇关于使用枚举作为地图的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文