# collections Companion code for the ankurm.com post *"How to Sort a HashMap by Value (and Key) in Java."* Module `collections` in `java-core-examples`. All explanation lives in the post; this module holds the runnable evidence and the captured output. ## Versions | Component | Version | |---|---| | JDK | 25.0.4.1+1 (Temurin, LTS) | | JMH | 1.37 | | JUnit Jupiter | 5.11.0 | | Hardware | 2 vCPU x86-64 VM, shared with other jobs (timings are indicative, not a leaderboard) | ## Quickstart ```bash export JDK25_HOME=/path/to/jdk-25 ./scripts/run-all.sh # rebuilds and regenerates everything in output/ ``` ## What is in here | File | Shows | Output | |---|---|---| | `SortByValueDemo` | stream into `LinkedHashMap`, descending, by key, list of entries, the `toMap()` default-HashMap trap, tie-break | `01` | | `TieBugDemo` | a `TreeMap` with a value-only comparator silently drops equal values; the tie-break fix | `02` | | `TopNDemo` | top-N by sort+limit and by a size-N `PriorityQueue` | `03` | | `SequencedDemo` | JDK 21+ `reversed()`, `firstEntry()`, `putLast()`, live-view behaviour | `04` | | `src/broken/*.java` | `HashMap` has no `firstEntry()`; `Collections.sort(map)` does not compile; javac's real message is captured | `05`-`06` | | `SortingBenchmark` | JMH: sort-all vs heap top-10 at 1,000 and 100,000 entries; full sort into `LinkedHashMap` | `07` | | `SortingTest` | 6 assertions behind the claims above | `08` | The JMH run is 2 forks, 3 warmup and 5 measurement iterations of 2 s per fork. The sandbox it ran on was shared with other jobs, so re-running moves the numbers by tens of percent but not the ordering.