我有一个
HashMap<String,Integer> map
我想按值按降序排序:
HashMap<String,Integer> topSorted = new HashMap<>();
//sort map in descending order
Stream<HashMap.Entry<String,Integer>> st = map.entrySet().stream();
st.sorted(Comparator.comparing((HashMap.Entry<String,Integer> e) -> e.getValue()).reversed())
.forEach(e -> topSorted.put(e.getKey(),e.getValue()));
但是topsorted仍然与map相同,根本不做任何排序
有人可以解释我做错了吗
HashMap
没有命令。您无法对其进行排序。
您可以使用LinkedHashMap
,它为您提供特定的迭代顺序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句