TreeMap如何按照Value进行排序

时间:2025-04-04 09:00:42
/** * 输出分词结果 和词频 */ private void printResult() { /**将TreeMap按照Value进行排序 最终结果词频高的在前*/ List<Map.Entry<String, Integer>> wordsList = new ArrayList<Map.Entry<String, Integer>>(wordsMap.entrySet()); wordsList.sort(new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { //降序排序 return o2.getValue().compareTo(o1.getValue()); /* * //升序排序 return ().compareTo(()); */ } }); System.out.println(wordsList.toString()); }