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?
Makoto
107k28 gold badges199 silver badges236 bronze badges
-
4Please edit your question to include your current code.Elliott Frisch– Elliott Frisch02/11/2014 04:35:47Commented Feb 11, 2014 at 4:35
4 Answers 4
Use values()
method.
HashMap<K,V> map = ...;
for(V v : map.values()){
// Use v
}
answered Feb 11, 2014 at 4:38
user2110286user2110286
Use it by this example:
Map<Integer, String> exampleMap = new HashMap<Integer, String>();
for (String value : exampleMap.values()) {
System.out.println("Value : " + value);
}
-
There is another similar question Check This OutD3X– D3X02/11/2014 05:54:52Commented Feb 11, 2014 at 5:54
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
}
answered Feb 11, 2014 at 5:12
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.
lang-java