Mapmap = new HashMap<>(); map.put("apple", 2); map.put("banana", 3); map.put("orange", 4); for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); }
MapThis code iterates over the map entries, checks if the key is "apple", and changes its value to "green". The resulting output is a modified map with the "apple" key pointing to "green". Both of these examples are part of the java.util package library in Java. Specifically, the Map interface and its associated methods are part of the java.util package.map = new HashMap<>(); map.put("apple", "red"); map.put("banana", "yellow"); map.put("orange", "orange"); for (Map.Entry entry : map.entrySet()) { if (entry.getKey().equals("apple")) { entry.setValue("green"); } } System.out.println(map);