JavaHow To Loop Through a HashMap
Loop Through a HashMap
Loop through the items of aHashMap
with afor-each loop.
Note: Use thekeySet()
method if you only want the keys, and use thevalues()
method if you only want the values:
Example
// Print keysfor (String i : capitalCities.keySet()) { System.out.println(i);}
Example
// Print valuesfor (String i : capitalCities.values()) { System.out.println(i);}
Example
// Print keys and valuesfor (String i : capitalCities.keySet()) { System.out.println("key: " + i + " value: " + capitalCities.get(i));}