我有一个与此类似的 HashMap:
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(3, 2);
map.put(7, 2);
map.put(5, 1);
map.put(10, 4);
Run Code Online (Sandbox Code Playgroud)
我需要先按值排序,然后按键排序,以防多个键共享相同的值。
结果应如下所示:
(5, 1)
(3, 2)
(7, 2)
(10, 4)
Run Code Online (Sandbox Code Playgroud)
请问有什么建议吗?
我首先比较值,仅在出现重复值的情况下才比较键。所以我同时使用键和值,而不仅仅是值。