java 静态 HashMap 初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35762823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Static HashMap Initialization
提问by Marco Aviles
I have this class, I want to know:
1° Is this the best way to define a static HashMasp
2° Is this the best way to do it in a Spring based app?(does Spring offer a better way to do this?)
我有这门课,我想知道:
1° 这是定义静态 HashMasp 的最佳方法吗
2° 这是在基于 Spring 的应用程序中实现它的最佳方法吗?(Spring 是否提供了更好的方法来做到这一点?)
Thanks in advance!
提前致谢!
public class MyHashMap {
private static final Map<Integer, String> myMap;
static {
Map<CustomEnum, String> aMap = new HashMap<CustomEnum, String>();
aMap.put(CustomEnum.UN, "one");
aMap.put(CustomEnum.DEUX, "two");
myMap = Collections.unmodifiableMap(aMap);
}
public static String getValue(CustomEnum id){
return myMap.get(id);
}
}
System.out.println(MyHashMap.getValue(CustomEnum.UN));
回答by Stephen84s
There are a few approaches to doing this. For example if your map is immutable you could consider using the Google Guava librariesIt has an ImmutableMap class which could be used to construct your map as:-
有几种方法可以做到这一点。例如,如果您的地图是不可变的,您可以考虑使用Google Guava 库它有一个 ImmutableMap 类,可用于将您的地图构造为:-
static final ImmutableMap<String, Integer> WORD_TO_INT =
new ImmutableMap.Builder<String, Integer>()
.put("one", 1)
.put("two", 2)
.put("three", 3)
.build();
If you are already using the Spring Frameworkand wiring your beans using XML, then you could fill the map in directly via the XML as :-
如果您已经在使用Spring Framework并使用 XML 连接 bean,那么您可以直接通过 XML 将映射填充为:-
...
<!-- creates a java.util.Map instance with the supplied key-value pairs -->
<util:map id="emails">
<entry key="pechorin" value="[email protected]"/>
<entry key="raskolnikov" value="[email protected]"/>
<entry key="stavrogin" value="[email protected]"/>
<entry key="porfiry" value="[email protected]"/>
</util:map>
...
回答by Gurpreet Singh
Either you can use Guava library. But if you dont want to use 3rd party library, then there are two ways to do it:
您可以使用番石榴库。但是如果你不想使用 3rd 方库,那么有两种方法可以做到:
Static initializer
private static final Map<String,String> myMap = new HashMap<String, String>(); static { myMap.put(key1, value1); myMap.put(key2, value2); } public static Map getMap() { return Collections.unmodifiableMap(myMap); }
Instance initialiser (anonymous subclass).
private static final Map<String,String> myMap = new HashMap<String, String>() { { put(key1, value1); put(key2, value2); } }; public static Map getMap() { return Collections.unmodifiableMap(myMap); } private static void addPair(String key, String val){ // Add key val to map }
静态初始化器
private static final Map<String,String> myMap = new HashMap<String, String>(); static { myMap.put(key1, value1); myMap.put(key2, value2); } public static Map getMap() { return Collections.unmodifiableMap(myMap); }
实例初始化程序(匿名子类)。
private static final Map<String,String> myMap = new HashMap<String, String>() { { put(key1, value1); put(key2, value2); } }; public static Map getMap() { return Collections.unmodifiableMap(myMap); } private static void addPair(String key, String val){ // Add key val to map }
Suppose later on you want to add some constant to map, then you can do that as well.
假设稍后您想向映射添加一些常量,那么您也可以这样做。
Collections.unmodifiableMap: It helps to have a unmodifiable view of map, which can not be modified. As it gives unsupported-Exception, if any modification is made to map.
Collections.unmodifiableMap:它有助于拥有无法修改的地图的不可修改视图。因为它给出了不受支持的异常,如果对映射进行了任何修改。