collections: companion code for the How to Sort a HashMap by Value (and Key) post

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KqJyCidz3ZgRyHABv2GVJh
This commit is contained in:
Claude
2026-09-30 19:10:13 +00:00
parent 366579f478
commit 381f119308
22 changed files with 509 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
HashMap order : {carol=85, dave=70, bob=70, alice=90, erin=95}
by value asc : {dave=70, bob=70, carol=85, alice=90, erin=95}
by value desc : {erin=95, alice=90, carol=85, dave=70, bob=70}
by key (Tree) : {alice=90, bob=70, carol=85, dave=70, erin=95}
toMap() no LHM: {carol=85, dave=70, bob=70, alice=90, erin=95} (class HashMap)
List<Entry> : [erin=95, alice=90, carol=85, dave=70, bob=70]
value desc+key: {erin=95, alice=90, carol=85, bob=70, dave=70}
+4
View File
@@ -0,0 +1,4 @@
input size : 5
TreeMap(value): {dave=70, carol=85, alice=90, erin=95} size 4
containsKey : bob true, dave true get(bob) = 70
TreeMap fixed : {bob=70, dave=70, carol=85, alice=90, erin=95} size 5
+2
View File
@@ -0,0 +1,2 @@
sort + limit(3): [erin=95, alice=90, carol=85]
heap of size 3 : [erin=95, alice=90, carol=85]
+9
View File
@@ -0,0 +1,9 @@
ascending : {bob=70, carol=85, alice=90, erin=95}
reversed() : {erin=95, alice=90, carol=85, bob=70}
firstEntry() : bob=70
lastEntry() : erin=95
top2 (by pos) : [erin=95, alice=90]
after put via view (no longer sorted): {bob=70, carol=85, alice=90, erin=95, zed=99}
putLast/putFirst : {carol=85, alice=90, erin=95, zed=99, bob=70}
TreeMap first : alice=90 reversed: {zed=99, erin=95, carol=85, bob=70, alice=90}
pollFirstEntry: carol=85 -> {alice=90, erin=95, zed=99, bob=70}
@@ -0,0 +1,6 @@
src/broken/HashMapIsNotSequenced.java:8: error: cannot find symbol
Map.Entry<String, Integer> first = m.firstEntry();
^
symbol: method firstEntry()
location: variable m of type HashMap<String,Integer>
1 error
@@ -0,0 +1,13 @@
src/broken/SortMapDirectly.java:8: error: no suitable method found for sort(Map<String,Integer>)
java.util.Collections.sort(m);
^
method Collections.<T#1>sort(List<T#1>) is not applicable
(cannot infer type-variable(s) T#1
(argument mismatch; Map<String,Integer> cannot be converted to List<T#1>))
method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>)
T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)
1 error
+7
View File
@@ -0,0 +1,7 @@
Benchmark (size) Mode Cnt Score Error Units
SortingBenchmark.heapTopN 1000 avgt 10 10.344 ± 2.238 us/op
SortingBenchmark.heapTopN 100000 avgt 10 642.111 ± 92.327 us/op
SortingBenchmark.sortAllIntoLinkedHashMap 1000 avgt 10 121.877 ± 11.882 us/op
SortingBenchmark.sortAllIntoLinkedHashMap 100000 avgt 10 46582.889 ± 9003.854 us/op
SortingBenchmark.sortAllThenLimit 1000 avgt 10 55.609 ± 5.247 us/op
SortingBenchmark.sortAllThenLimit 100000 avgt 10 35128.268 ± 10045.080 us/op
+4
View File
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.ankurm.collections.SortingTest
-------------------------------------------------------------------------------
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.229 s -- in com.ankurm.collections.SortingTest