English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this example, we will learn to iterate over the keys, values, and entries of Java HashMap/value mapping.
To understand this example, you should be familiar with the followingJava ProgrammingTopic:
In Java HashMap, we can iterate over itskeys,valuesandkey / valueMapping.
import java.util.HashMap; import java.util.Map.Entry; class Main { public static void main(String[] args) { //Create a HashMap HashMap<String, String> languages = new HashMap<>(); languages.put("Java", "Enterprise"); languages.put("Python", "ML/AI"); languages.put("JavaScript", "Frontend"); System.out.println("HashMap: "); + languages); //Traverse keys/Value mapping System.out.print("Entries: "); for(Entry<String, String> entry : languages.entrySet()) { System.out.print(entry); System.out.print(", "); } //Traverse keys System.out.print("\nKeys: "); for(String key : languages.keySet()) { System.out.print(key); System.out.print(", "); } // Traverse values System.out.print("\nValues: "); for(String value : languages.values()) { System.out.print(value); System.out.print(", "); } } }
Output result
HashMap: {Java=Enterprise, JavaScript=Frontend, Python=ML}/AI} Entries: Java=Enterprise, JavaScript=Frontend, Python=ML/AI, Keys: Java, JavaScript, Python, Values: Enterprise, Frontend, ML/AI,
In the above example, we created a hash map named languages. Here, we use the forEach loop to iterate over the elements of the hash map.
Note that we traverse independentlykey,valuesandkey / valueMapping.
language.entrySet() - Returns a set collection view of all entries
language.keySet() -Returns a set collection view of all keys
language.values() -Returns a set collection view of all values
Note:We have used the Map.Entry class. The nested class returns a map view.
import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; class Main { public static void main(String[] args) { //Create a HashMap HashMap<String, String> languages = new HashMap<>(); languages.put("Java", "Enterprise"); languages.put("Python", "ML/AI"); languages.put("JavaScript", "Frontend"); System.out.println("HashMap: "); + languages); //Create an Iterator object Iterator<Entry<String, String>> iterate1 = languages.entrySet().iterator(); //Traverse keys/Value mapping System.out.print("Entries: "); while(iterate1.hasNext()) { System.out.print(iterate1.next()); System.out.print(", "); } //Traverse keys Iterator<String> iterate2 = languages.keySet().iterator(); System.out.print("\nKeys: "); while(iterate2.hasNext()) { System.out.print(iterate2.next()); System.out.print(", "); } //Traverse values Iterator<String> iterate3 = languages.values().iterator(); System.out.print("\nValues: "); while(iterate3.hasNext()) { System.out.print(iterate3.next()); System.out.print(", "); } } }
Output result
HashMap: {Java=Enterprise, JavaScript=Frontend, Python=ML}/AI} Entries: Java=Enterprise, JavaScript=Frontend, Python=ML/AI, Keys: Java, JavaScript, Python, Values: Enterprise, Frontend, ML/AI,
In the above example, we traverse the keys, values, and entries of the hash map./Value mapping. We used the iterator() method to iterate over the hash map. Here,
hasNext() - Returns true if there is a next element in the hashmap.
next() - Return the next element of the hash map.
Note: We can also useHashMap forEach()Method to iterate over a hash map.