开发者

convert a hashmap to an array [duplicate]

开发者 https://www.devze.com 2023-02-17 15:36 出处:网络
This question already has answers here: 开发者_StackOverflow社区Java: how to convert HashMap<String, Object> to array
This question already has answers here: 开发者_StackOverflow社区 Java: how to convert HashMap<String, Object> to array (14 answers) Closed 6 years ago.

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?


See same question here:

hashMap.keySet().toArray(); // returns an array of keys
hashMap.values().toArray(); // returns an array of values


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...


I guess, you just need to write a method, that does what you want.


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).

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:

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).

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.

I hope this helps, please let me know if you need any additional info.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号