for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
Map<String, Object> map = ...;
for (String key : map.keySet()) {
// ...
}
HashMap<Integer, Integer> table = new HashMap<>();
for(Map.Entry<Integer, Integer> key: table.entrySet()){
System.out.println(key.getKey() + " " + key.getValue());
}
// 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);
}