Skip to content

Instantly share code, notes, and snippets.

@EduardinDev
Created September 14, 2021 06:04
Show Gist options
  • Save EduardinDev/122909d1b8b53b2bd2d5905b9533b07f to your computer and use it in GitHub Desktop.
Save EduardinDev/122909d1b8b53b2bd2d5905b9533b07f to your computer and use it in GitHub Desktop.
Tipos de iteraciones en la colección HashMap de java
public static void main (String[] args) {
//a map with key type : String, value type : String
Map<String,String> mp = new HashMap<String,String>();
mp.put("John","Math"); mp.put("Jack","Math"); map.put("Jeff","History");
//3 differents ways to iterate over the map
for (String key : mp.keySet()){
//iterate over keys
System.out.println(key+" "+mp.get(key));
}
for (String value : mp.values()){
//iterate over values
System.out.println(value);
}
for (Entry<String,String> pair : mp.entrySet()){
//iterate over the pairs
System.out.println(pair.getKey()+" "+pair.getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment