Mapmap = new HashMap<>(); map.put("apple", 5); map.put("banana", 8); map.put("orange", 3); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); }
MapThis code creates a Map of strings to strings, adds an entry, and then retrieves the first entry using an iterator. It then modifies the value associated with the key using setValue(). Finally, it prints out the new value associated with the key. These examples use the java.util package library, which is included in the standard Java library.map = new HashMap<>(); map.put("hello", "world"); Map.Entry entry = map.entrySet().iterator().next(); entry.setValue("everyone"); System.out.println(map.get("hello"));