Created
September 14, 2021 06:04
-
-
Save EduardinDev/122909d1b8b53b2bd2d5905b9533b07f to your computer and use it in GitHub Desktop.
Tipos de iteraciones en la colección HashMap de java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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