Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java hashmap key iteration

Is there any way to iterate through a java Hashmap and print out all the values for every key that is a part of the Hashmap?

like image 941
ping Avatar asked Jul 27 '10 14:07

ping


1 Answers

Yes, you do this by getting the entrySet() of the map. For example:

Map<String, Object> map = new HashMap<String, Object>();

// ...

for (Map.Entry<String, Object> entry : map.entrySet()) {
    System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
}

(Ofcourse, replace String and Object with the types that your particular Map has - the code above is just an example).

like image 62
Jesper Avatar answered Sep 29 '22 13:09

Jesper