Java Hashmap values to Array

拈花ヽ惹草 提交于 2019-12-11 02:55:44

问题


I have the following code which adds some arrays to a hashmap but then I want access those arrays to do some work on them later. I've gotten this far but can't figure the rest out to make it work....

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

    public static void main(String[] args) {

        String[][] layer1 = {
            {"to1", "TYPE1", "start"}, 
            {"to2", "TYPE1", "start"}
        };

        String[][] layer2 = {
            {"to3", "TYPE2" ,"item1"},
            {"to3", "TYPE2" ,"item2"}
        };

        HashMap<String,Object> hashMap = new HashMap<String,Object>();
        hashMap.put("layer1", layer1);
        hashMap.put("layer2", layer2);

        Iterator<Entry<String, Object>> iterator = hashMap.entrySet().iterator();
        while(iterator.hasNext()){
            hashMap.values().toArray();
            for (???) {
                // lets print array here for example
            }
        }        

    }

回答1:


Smells like homework, but a few suggestions -

  • there's no reason for your Hashmap to be of the form <String,Object> - make it <String, String[][]> , as that's what you're storing.

You're iterating twice. You either - iterate through the map, either the keys, values or entries. Each item is an iterator return value, e.g.

for (String[][] s:map.values()){
 ...
}
  • hashmap.values.toArray gives you all of the contents, which is the same thing your iterator is doing.

  • if you're only iterating through the contents, then you're not really using a map, as you're never making use of the fact that your values are available by key.




回答2:


... just mixing too many languages to learn atm

Can I suggest that you need to stop and take the time to learn Java properly. Your code looks like you are trying to write "perlish" ... associative arrays and arrays instead of proper types. The end result is Java code that is slow and fragile compared with code that is designed and written using the Java mindset.

It might seem like you are being productive, but what you are producing is likely to be problematic going forward.




回答3:


Your while loop should look like -

        while(iterator.hasNext()){
            String[][] arr = (String[][])iterator.next().getValue();

            for (String[] strings : arr) {
                for (String string : strings) {
                    System.out.println(string);
                }
            }
        }



回答4:


Your code is bad formed here:

while(iterator.hasNext()){
    hashMap.values().toArray();
    for (???) {
        // lets print array here for example
    }
}      

You try to iterate using the Iterator "iterator" but next you call to

hashMap.values().toArray();

To get the next item of the loop you need to use iterator.next(); to fetch it. Also is good to change the "Object" by String[][] or to List<String[]> or List<List<String>>.

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Map.Entry;

public static void main(String[] args) {

    String[][] layer1 = {
        {"to1", "TYPE1", "start"}, 
        {"to2", "TYPE1", "start"}
    };

    Map<String,String[][]> map= new HashMap<String,String[][]>();
    map.put("layer1", layer1);

    Iterator<Entry<String, String[][]>> iterator = map.entrySet().iterator();
    while(iterator.hasNext()){
        Entry<String, String[][]> entry = iterator.next();
        System.out.println("Key:" + entry.getKey());

        String[][] value = entry.getValue();
        for(int x=0;x<value.length;x++){
         for(int y=0;y<value[x].length;y++){
          System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);        
         }
        }
    }        
}

Also you can use "for each" loop to simplify the code insted using the "while":

for (Entry<String, String[][]> entry : map.entrySet()){
    System.out.println("Key:" + entry.getKey());
    String[][] value = entry.getValue();
    for(int x=0;x<value.length;x++){
        for(int y=0;y<value[x].length;y++){
            System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);      
        }
    }
}

Or if you only need the values:

for (Entry<String, String[][]> entry : map.values()){
    String[][] value = entry.getValue();
    for(int x=0;x<value.length;x++){
        for(int y=0;y<value[x].length;y++){
            System.out.println("String[" + x + "][" + y + "]:" + value[x][y]);      
        }
    }
}


来源:https://stackoverflow.com/questions/3455449/java-hashmap-values-to-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!