How to properly iterate over all elements of a HashMap in Java? Different iteration methods
Hello everyone! I'm a beginner Java developer, and I have a problem with iterating over a HashMap. \n\nI have a HashMap that stores student names and their grades:\n\n```java\nHashMap studentGrades = new HashMap<>();\nstudentGrades.put("Ivan", 85);\nstudentGrades.put("Maria", 92);\nstudentGrades.put("Petr", 78);\nstudentGrades.put("Anna", 95);\n```\n\nI need to display all students and their grades on the screen. I tried using a regular for-each loop, but it doesn't work directly with HashMap. \n\nThen I found an example on the internet using keySet():\n\n```java\nfor (String name : studentGrades.keySet()) {\n System.out.println(name + ": " + studentGrades.get(name));\n}\n```\n\nThis works, but I heard that this method is inefficient, especially for large maps. I also saw that someone