MapscoreMap = new HashMap<>(); scoreMap.put("John", 80); scoreMap.put("Mary", 90); for (Map.Entry entry : scoreMap.entrySet()) { System.out.println(entry.getKey() + " scored " + entry.getValue()); }
import java.util.TreeMap; MapThis code declares a TreeMap called treeMap, adds three key-value pairs to it in ascending order of key, and then loops through all the entries in the map using the entrySet() method. For each entry, it prints the key and value. Note that TreeMap is part of the java.util package. In summary, the java.util.map Entry interface is used to work with key-value pairs in a Map, and is part of the Java collections framework. It is used with other classes and interfaces in the java.util package, such as HashMap and TreeMap.treeMap = new TreeMap<>(); treeMap.put(2, "Two"); treeMap.put(1, "One"); treeMap.put(3, "Three"); for (Map.Entry entry : treeMap.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); }