Java Hashmap 值到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3455449/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:56:27  来源:igfitidea点击:

Java Hashmap values to Array

javaarrayshashmap

提问by EddyR

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

    }

回答by Steve B.

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.
  • 您的 Hashmap 没有理由采用这种形式<String,Object>- make it <String, String[][]>,因为这就是您要存储的内容。

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.

  • hashmap.values.toArray 为您提供所有内容,这与您的迭代器正在做的事情相同。

  • 如果您只是遍历内容,那么您并没有真正使用地图,因为您从来没有利用过您的值可以通过键获得的事实。

回答by Stephen C

... just mixing too many languages to learn atm

...只是混合了太多语言来学习atm

Can I suggest that you need to stopand 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.

我是否建议您需要停下来花时间正确学习 Java。您的代码看起来像是在尝试编写“perlish”...关联数组和数组,而不是正确的类型。与使用 Java 思维方式设计和编写的代码相比,最终结果是 Java 代码既缓慢又脆弱。

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

看起来您的工作效率很高,但您所生产的产品在未来可能会出现问题。

回答by Gopi

Your while loop should look like -

你的 while 循环应该看起来像 -

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

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

回答by Dubas

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

要获取循环的下一项,您需要使用 iterator.next(); 去取它。通过 String[][] 或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 each”循环来简化使用“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]);      
        }
    }
}