简体   繁体   中英

HashMap Iteration using a for loop

I have String array values in a hash map. I need to retrieve that using for loop and store it in a String array. Could anybody show me how with a simple example?

Use values() method.

HashMap<K,V> map = ...;
for(V v : map.values()){
    // Use v 
}

Use it by this example:

Map<Integer, String> exampleMap = new HashMap<Integer, String>();
for (String value : exampleMap.values()) {
            System.out.println("Value : " + value);
     }

Taking your question literally, it seems you have:

Map<String, String[]> map; 

You haven't said what the key type is - assuming String, but doesn't matter for this.

To iterate over the values:

for (String[] array : map.values()) {
    // do something with array
}
HashMap<Integer,String> hm;
int i=0;
for(Map.Entry<Integer,String> e : hm.entrySet())
{
    stringArray[i] = e.getValue();
    i++;

}

Assuming that you have Integer keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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