The provided code demonstrates various ways to iterate over a Map in Java to print its keys and values. Each method showcases different Java features and versions, ranging from using traditional Iterator to more modern constructs introduced in Java 8 and Java 10. Here's an explanation of each block of code:
Using Iterator and Map.Entry
java
Copy code
Iterator<Map.Entry<String, Integer>> it = mapCountryCode.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,Integer> value = it.next();
System.out.println("Key:" + value.getKey() + ", Value:" + value.getValue());
}
Explanation:
mapCountryCode.entrySet().iterator() returns an Iterator over the entrySet of the map.
The while loop iterates as long as there are more elements (it.hasNext()).
Inside the loop, it.next() retrieves the next Map.Entry object, which represents a key-value pair.
The key and value are accessed using value.getKey() and value.getValue(), respectively, and printed.
Using Iterator and var (Java 10)
java
Copy code
var its = mapCountryCode.entrySet().iterator();
while(its.hasNext()){
var value = its.next();
System.out.println("Key:" + value.getKey() + ", Value:" + value.getValue());
}
Explanation:
This is similar to the previous block but uses the var keyword introduced in Java 10.
var allows the compiler to infer the type of the variable automatically.
Using foreach and Map.Entry
java
Copy code
for (Map.Entry<String, Integer> pair : mapCountryCode.entrySet()){
System.out.println("Key:" + pair.getKey() + ", Value:" + pair.getValue());
}
Explanation:
A foreach loop is used to iterate over the entrySet of the map.
For each Map.Entry object in the set, the key and value are printed.
Using foreach and var (Java 10)
java
Copy code
for (var pair : mapCountryCode.entrySet()){
System.out.println("Key:" + pair.getKey() + ", Value:" + pair.getValue());
}
Explanation:
Similar to the previous foreach loop, but using var for type inference.
Using forEach from Java 8
java
Copy code
mapCountryCode.forEach((k, v) -> System.out.println("Key:" + k + ", Value:" + v));
Explanation:
The forEach method with a lambda expression is used to iterate over the map.
The lambda expression (k, v) -> System.out.println("Key:" + k + ", Value:" + v) takes each key-value pair and prints it.
Using keySet
java
Copy code
for (String keys : mapCountryCode.keySet()){
System.out.println("Key:" + keys + ", Value:" + mapCountryCode.get(keys));
}
Explanation:
Iterates over the keySet of the map.
For each key, it retrieves the corresponding value using mapCountryCode.get(keys) and prints the key-value pair.
Using keySet with forEach (Java 8)
java
Copy code
mapCountryCode.keySet().forEach(v -> System.out.println("Key:" + v + ", Value:" + mapCountryCode.get(v)));
Explanation:
Uses the forEach method on the keySet of the map with a lambda expression.
For each key, it retrieves and prints the key-value pair.
Using keySet and Iterator
java
Copy code
Iterator<String> itse = mapCountryCode.keySet().iterator();
while(itse.hasNext()){
String value = itse.next();
System.out.println("Key:" + value + ", Value:" + mapCountryCode.get(value));
}
Explanation:
Uses an Iterator to iterate over the keySet of the map.
The while loop iterates through each key, retrieves the corresponding value using mapCountryCode.get(value), and prints the key-value pair.
Each method demonstrates different ways to iterate through a Map in Java, leveraging different language features and versions to achieve the same goal: printing all key-value pairs in the map.
Watch video Iterate over each entry in a Java Map - using entryset keyset in java 10 java 8 online, duration hours minute second in high quality that is uploaded to the channel CodeWis Technologies by Nuhman Paramban 24 May 2024. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 32 times and liked it 0 visitors.