How to sort a Map by values in Java?
Hello everyone! I'm new to Java and ran into a problem. I have a `Map`, where keys are student names and values are their scores. I need to output students in ascending order of scores (from lowest to highest).
The problem is that standard Map implementations (e.g., `HashMap` or `TreeMap`) only sort by keys. I tried using `TreeMap` with a comparator, but it only works with keys.
Here's an example of my code:
```java
Map scores = new HashMap<>();
scores.put("Alice", 85);
scores.put("Bob", 92);
scores.put("Charlie", 78);
scores.put("David", 88);
// Trying to sort by values (scores)
// I want to get: Charlie=78, Alice=85, David=88, Bob=92
```
I've read that you can use `Stream API` or a `List` of `Map.Entry` to achieve this.