简体   繁体   English

获取HashMap的所有子级 <Integer, ArrayList<String> &gt;

[英]Get all child of HashMap<Integer, ArrayList<String>>

I use this code for add to hashmap : 我使用此代码添加到hashmap

static HashMap<Integer, ArrayList<String>> arrayMap = new HashMap<Integer, ArrayList<String>>();

ArrayList<String> list = new ArrayList<String>();
list.add("t");
list.add("b");
arrayMap.put(12, list);

ArrayList<String> list = new ArrayList<String>();
list.add("y");
list.add("x");
arrayMap.put(18, list);

this code work correct 此代码正常工作

but i cant get child of arrayMap in foreach 但是我无法在foreach中获得arrayMaparrayMap

for(arrayMap .... ){
     return // 12:t,b   18:y,x
}

My try : 我的尝试:

for (int a = 0; a < arrayMap.size(); a++)
        {
            HashMap<String, Integer> tmpData = (HashMap<String, Integer>) arrayMap.get(a);
            Set<String> key = tmpData.keySet();
            Iterator it = key.iterator();
            while (it.hasNext()) {
                String hmKey = (String) it.next();
                Integer hmData = (Integer) tmpData.get(hmKey);

                System.out.println("Key: " + hmKey + " & Data: " + hmData);
                it.remove(); // avoids a ConcurrentModificationException
            }

        }

You'll want to iterate over the each entry in the HashMap in a nested loop like this: 您需要像这样在嵌套循环中遍历HashMap中的每个条目:

for(Map.Entry<Integer, ArrayList<String>> entry : arrayMap.entrySet()){
   //entry.getKey() will be each key in the HashMap
    for(String str : entry.getValue()){
         // str is one entry of the ArrayList for that Key
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM