for(Map.Entry<String, Integer> entry : hashMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
//do something with the key and value
}
// Creating a HashMap
Map<String, String> langTable = new HashMap<String, String>();
// Inserting elements to HashMap
langTable.put("A", "Angular");
langTable.put("J", "Java");
langTable.put("P", "Python");
langTable.put("H", "Hibernate");
// Iterating HashMap using foreach
for (Map.Entry<String, String> set : langTable.entrySet()) {
// Printing all elements of a Map
System.out.println(set.getKey() + " = " + set.getValue());
}
Map<Character, String> charType
= new HashMap<Character, String>();
// Inserting data in the hash map.
charType.put('J', "Java");
charType.put('H', "Hibernate");
charType.put('P', "Python");
charType.put('A', "Angular");
// Iterating HashMap through forEach and
// Printing all. elements in a Map
charType.forEach(
(key, value)
-> System.out.println(key + " = " + value));
// or
// using for loop to iterate the map entry sets
for (Map.Entry<String,String> mapElement : charType.entrySet()) {
String key = mapElement.getKey();
// Adding some bonus marks to all the students
String value = mapElement.getValue();
// Printing above marks corresponding to
// students names
System.out.println(key + " : " + value);
}