for(Map.Entry<String,Integer> entry : hashMap.entrySet()){String key = entry.getKey();Integer value = entry.getValue();//do something with the key and value}
// Java program to demonstrate iteration over// Map.entrySet() entries using for-each loopimportjava.util.Map;importjava.util.HashMap;classIterationDemo{publicstaticvoidmain(String[] arg){Map<String,String> gfg =newHashMap<String,String>();// enter name/url pair
gfg.put("GFG","geeksforgeeks.org");
gfg.put("Practice","practice.geeksforgeeks.org");
gfg.put("Code","code.geeksforgeeks.org");
gfg.put("Quiz","quiz.geeksforgeeks.org");// using for-each loop for iteration over Map.entrySet()for(Map.Entry<String,String> entry : gfg.entrySet())System.out.println("Key = "+ entry.getKey()+", Value = "+ entry.getValue());}}
// Creating a HashMapMap<String,String> langTable =newHashMap<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 foreachfor(Map.Entry<String,String> set : langTable.entrySet()){// Printing all elements of a MapSystem.out.println(set.getKey()+" = "+ set.getValue());}