Avatar ·

How to efficiently iterate over all entries in a Java Map?

Hi everyone! I'm a beginner Java developer, and I have a problem with iterating over elements in a HashMap. I need to go through all keys and values to, for example, display them on the screen or perform some calculations. Currently I'm doing it like this: ```java Map map = new HashMap<>(); map.put("apple", 1); map.put("banana", 2); map.put("cherry", 3); for (String key : map.keySet()) { Integer value = map.get(key); System.out.println(key + " -> " + value); } ``` This works, but I've heard there are more efficient ways. My code makes two calls to the map on each iteration: first it gets the key, then it gets the value by that key. Wouldn't it be faster if I iterated over key-value pairs directly? I know that Java has the Map.Entry interface, but I'm not entirely