Java 按值对哈希映射进行排序
我有这个哈希地图:
HashMap<String, Integer> m
它基本上存储任何单词(字符串)及其频率(整数)。下面的代码按值对哈希映射进行排序:
public static Map<String, Integer> sortByValue(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> m1, Map.Entry<String, Integer> m2) {
return (m2.getValue()).compareTo(m1.getValue());
}
});
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
现在情况已经改变,我有这个:
HashMap<String, doc>;
class doc{
integer freq;
HashMap<String, Double>;
}
我如何按值对此HashMap进行排序,遵循与sortByValue相同的方法?