/**
* 根据value对map进行排序
*/
public Map sortAllMapByValue(Map map) {
// 这里将map.entrySet()转换成list
List> list = new ArrayList>(map.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list,new Comparator>() {
// 降序排序
public int compare(Entry o1, Entry o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
// 存入新的map返回
Map newMap = new LinkedHashMap();
Iterator> iter = list.iterator();
Map.Entry tmpEntry = null;
int count = 0;
while (iter.hasNext()) {
tmpEntry = iter.next();
count++;
if (count > 100) { // 最多存入100个关键词
iter.remove();
} else {
newMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
}
return newMap;
}