-1

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
asked Feb 11, 2014 at 4:32
1
  • 4
    Please edit your question to include your current code. Commented Feb 11, 2014 at 4:35

4 Answers 4

1

Use values() method.

HashMap<K,V> map = ...;
for(V v : map.values()){
 // Use v 
}
answered Feb 11, 2014 at 4:38
1

Use it by this example:

Map<Integer, String> exampleMap = new HashMap<Integer, String>();
for (String value : exampleMap.values()) {
 System.out.println("Value : " + value);
 }
answered Feb 11, 2014 at 4:45
1
  • There is another similar question Check This Out Commented Feb 11, 2014 at 5:54
1

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

answered Feb 11, 2014 at 4:57
2
  • can u pls explain this if i have more keys with same string array values..same code will work out? Commented Feb 11, 2014 at 5:05
  • Yes, it doesn't matter if you have same String values. Commented Feb 11, 2014 at 6:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.