假设我有一个包含100个值的枚举.为简单起见,请参考以下示例:
public enum code
{
CODE_1("string1"),
CODE_2("string2"),
CODE_3("string3"),
CODE_4("string4"),
...
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个公共方法与已知的格式字符串(如"字符串1","字符串2" ......)转换中选取适当的枚举值CODE_1,CODE_2 ......这通常是通过遍历所有的值来实现的,如果找到匹配项,返回该枚举值.(详情可在此问题中找到.)
但是,我关注的是对所有值进行reguraly循环.这可能是一个巨大的瓶颈吗?如果不是100个元素,那么有1000个?
作为我自己的练习,我尝试使用静态地图优化此查找,这可以确保给定任何字符串的O(1)查找时间.我喜欢这个额外的噱头,但我只想在我的代码中包含它,如果它确实是必要的.使用迭代方法和map方法有什么想法和发现?
public enum Code
{
...
//enum values
...
//The string-to-Code map
private static final Map<String,Code> CODE_MAP = populateMap();
private static Map<String,Code> populateMap()
{
Map<String,Code> map = new HashMap<String,Code>();
for(Code c : Code.values())
{
map.put(c.getCode(), c);
}
return map;
}
private String code;
private Code(String code)
{
this.code = code;
}
public String getCode()
{
return this.code;
}
public Code convertFromString(String code)
{
//assume that the given string is actually a key value in the map
return (Code) CODE_MAP.get(code);
}
}
Run Code Online (Sandbox Code Playgroud)
Boh*_*ian 41
You want a Map<String, Code>, but how to populate it neatly? Enums don't allow you to initialize a static fields before the enum instances are initialized, but there's a neat little trick, called the Initialization-on-demand holder idiom, that makes using a statically initialized map needed for this functionality easy to implement:
public enum Code {
CODE_1("string1"),
CODE_2("string2"),
CODE_3("string3"),
// etc
;
private static class Holder {
static Map<String, Code> CODE_MAP = new HashMap<>();
}
private final String code;
private Code(String code) {
this.code = code;
Holder.CODE_MAP.put(code, this);
}
public String getCode() {
return this.code;
}
public Code convertFromString(String code) {
return CODE_MAP.get(code);
}
}
Run Code Online (Sandbox Code Playgroud)
This works because the class loader initializes inner static classes before initializing the enum class, so the map is assigned ready to load during enum instance initialization.
没有循环.没有特殊的代码来加载地图(在构造函数中完成).最小的代码.
| 归档时间: |
|
| 查看次数: |
4337 次 |
| 最近记录: |