How to sort a Map by values in Java?
Hi everyone! I'm new to Java and encountered a problem: I need to sort a HashMap by values. I have a dictionary where the key is the student's name and the value is their grade. I need to output students in ascending order of grades.\n\nHere's my code:\n\n```java\nMap grades = new HashMap<>();\ngrades.put("Alice", 85);\ngrades.put("Bob", 92);\ngrades.put("Charlie", 78);\ngrades.put("Diana", 95);\n```\n\nI know that HashMap doesn't guarantee order, and TreeMap only sorts by keys. How can I get a sorted list of entries by values? I tried googling, but the solutions seem too complicated. Maybe there's a simple way to do this using Stream API or Collections? I'd appreciate a detailed example.\n\n<<>>\n