java 将哈希映射转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5322130/
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
convert a hashmap to an array
提问by tetsuya
I have a hashmap like this:
我有一个这样的哈希图:
HashMap<String, String[]> map = new HashMap<String, String[]>();
I want to convert this to an array, say Temp[], containing as first value the key from the hashmap and as second the String[]-array from the map, also as array. Is this possible? How?
我想将它转换为一个数组,比如 Temp[],它包含哈希映射中的键作为第一个值,第二个包含映射中的 String[]-array,也作为数组。这可能吗?如何?
回答by Benoit Thiery
回答by erikbwork
Hm. Your question is really strange, but here is what you asked:
嗯。你的问题真的很奇怪,但这是你问的:
HashMap<String, String[]> map = new HashMap<String, String[]>();
String[] keys = map.keySet().toArray();
Object[] result = new Object[keys.length*2]; //the array that should hold all the values as requested
for(int i = 0; i < keys.length; i++) {
result[i] = keys[i]; //put the next key into the array
result[i+1] = map.get(keys[i]); //put the next String[] in the array, according to the key.
}
But man, for what should you ever need something like this? Whatever you want to do, The chance is over 99% that you don't need to write something like this...
但是,伙计,你为什么需要这样的东西?无论你想做什么,你不需要写这样的东西的机会超过99%......
回答by weekens
I guess, you just need to write a method, that does what you want.
我想,您只需要编写一个方法,它就可以满足您的需求。
回答by byyyk
I'm not sure if this is what you needed but here is your code that I modified:
我不确定这是否是您需要的,但这是我修改过的代码:
Map<String, String[]> map = new HashMap<String, String[]>();
map.put("key1", new String[]{"value1", "test1"});
map.put("key2", new String[]{"value2"});
Object[] keys = map.keySet().toArray();
Object[] values = map.values().toArray();
System.out.println("key = " + keys[0] + ", value #1 = " + ((Object[])values[0])[0]);
System.out.println("key = " + keys[1] + ", value #1 = " + ((Object[])values[1])[0] + ", value #2 = " + ((Object[])values[1])[1]);
Output:
输出:
key = key2, value #1 = value2
key = key1, value #1 = value1, value #2 = test1
Note that under key[0]
you have "key2"
instead of "key1"
. That is because HashMap
doesn't keep the keys in the same order you add them. To change that you must choose another Map
implementation (for example TreeMap
if you want to have alphabetical order).
请注意,在key[0]
您之下有"key2"
而不是"key1"
. 那是因为HashMap
不会按照添加它们的相同顺序保留密钥。要更改它,您必须选择另一个Map
实现(例如,TreeMap
如果您想按字母顺序排列)。
How this works? Java allows you to cast arrays to Object
. toArray()
method returns Object[]
- an array of Objects
, but those objects are also arrays (of String - as you defined in your map)! If you print just value[0]
it would be like printing mySampleArray
where mySampleArray
is:
这是如何工作的?Java 允许您将数组转换为Object
. toArray()
方法返回Object[]
- 的数组Objects
,但这些对象也是数组(字符串 - 正如您在地图中定义的那样)!如果你只是打印value[0]
它就像打印mySampleArray
在哪里mySampleArray
:
Object[] mySampleArray = new Object[5];
So you call it on the whole array not an element. This is why you get [Ljava.lang.String;@7c6768
etc. (which is className@HashCode - thats what default toString()
method does).
所以你在整个数组上调用它而不是一个元素。这就是为什么你得到[Ljava.lang.String;@7c6768
等(这是 className@HashCode - 这就是默认toString()
方法所做的)。
In order to get to your element you have to go deeper.
为了达到你的元素,你必须更深入。
((Object[])values[0])[0]
This means: Take values[0] (we know it holds an array), cast it to array and take first element.
这意味着:取值[0](我们知道它包含一个数组),将其转换为数组并取第一个元素。
I hope this helps, please let me know if you need any additional info.
我希望这会有所帮助,如果您需要任何其他信息,请告诉我。