java中的HashMap初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26999663/
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
HashMap initialization in java
提问by zds
I have a question about HashMap
creation. Is there a simple and fast way of HashMap
creation? Maybe, concatenation of two arrays {1, 2, ...}
and {"picture/one.png", "picture/two.png", ...}
.
I am interested in a neat solution. Best practice, so to say.
我有一个关于HashMap
创作的问题。有没有简单快捷的HashMap
创作方式?也许,将两个数组{1, 2, ...}
和{"picture/one.png", "picture/two.png", ...}
. 我对一个简洁的解决方案感兴趣。最佳实践,可以这么说。
Every guidance or hint would be very helpful. Thanks.
每一个指导或提示都会非常有帮助。谢谢。
EDIT: And yes, I know how to initiate a HashMap
. And I looked in javadoc (not even once).
Sorry for bad explanation of my question, maybe it is not very clear. Once more, I am interested in best practice solution. If the best practice solution is a for-loop, so that's it. If there are other options, please, show.
编辑:是的,我知道如何启动HashMap
. 我查看了 javadoc(甚至一次都没有)。对不起,我的问题解释不好,也许不是很清楚。再一次,我对最佳实践解决方案感兴趣。如果最佳实践解决方案是 for 循环,就是这样。如果有其他选择,请展示。
采纳答案by gorootde
Yes it is possible:
对的,这是可能的:
public static <K,V> Map<K,V> mapFromArrays(K[] keys,V[]values){
HashMap<K, V> result=new HashMap<K, V>();
for(int i=0;i<keys.length;i++){
result.put(keys[i], values[i]);
}
return result;
}
Assuming that keys and values have the same length.
假设键和值具有相同的长度。
You may also use this function in a static initializer like this:
您也可以在静态初始化程序中使用此函数,如下所示:
private static Integer[] keys=new Integer[]{1,2,3};
private static String[] values=new String[]{"first","second","third"};
private static Map<Integer,String> myMap;
{
myMap=mapFromArrays(keys, values);
}
回答by user949300
The short answer is NO. However, you can come closewith varargs in a static utility function.
最简洁的答案是不。但是,您可以接近与静态效用函数可变参数。
With no error checking, and no generics:
没有错误检查,也没有泛型:
public static Map kvPairsToMap(Object...args) {
// TODO check that args has an even length
Map map = new HashMap();
for (int i=0; i<args.length; i+=2) {
map.put(args[i], args[i+1]);
}
return map;
}
Usage would be
用法是
Map dic = kvPairsToMap(1,"picture/one.png", 2,"picture/two.png", ...);
回答by user2367418
Here is a way to initialize a map using variable declarations when the keys and values are Strings.
当键和值是字符串时,这是一种使用变量声明来初始化映射的方法。
First declare a two dimensional array of Strings. Use the curly bracket notation to initialize the array, such as
首先声明一个二维字符串数组。使用大括号表示法来初始化数组,例如
final static String [][] animalEatsArray = {{"mouse", "cheese"}, {"dog", "bone"}};
Then declare and initialize the map:
然后声明并初始化地图:
final static Map animalEatsMap = buildMapFromStringArray(animalEatsArray );
You need a method like this somewhere:
您在某处需要这样的方法:
public static Map<String, String> buildMapFromStringArray( String [] [] stringArray) {
if (stringArray == null ) {
throw new IllegalArgumentException("buildMapFromStringArray: stringArray is null");
}
Map<String, String> map = new HashMap<String, String>( 1 + (2 * stringArray.length) );
for ( String[] keyValue : stringArray) {
map.put(keyValue[0], keyValue[1]);
}
return map;
}