Mapmap = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); }
MapThis code creates a TreeMap with some key-value pairs and then retrieves the first entry using the firstEntry() method. It then prints the key and value of the first entry using the getKey() and getValue() methods. The java.util.Map.Entry interface is part of the Java Collections Framework.map = new TreeMap<>(); map.put(1, "apple"); map.put(2, "banana"); map.put(3, "cherry"); Map.Entry firstEntry = map.firstEntry(); System.out.println("First entry: " + firstEntry.getKey() + ": " + firstEntry.getValue());