java编程心得(十三)——哈希表根据value值排序

来源:互联网 发布:原油ela数据 编辑:程序博客网 时间:2024/04/28 20:20
/** * 功能:排序 */private static void outputRegionStatistics(HashMap<String, Integer> regionMap){ ArrayList<Entry<String, Integer>> mappingList = new ArrayList<Map.Entry<String, Integer>>(regionMap.entrySet()); //通过比较器实现比较排序,当前排序结果是升序,若需降序只用将return中的mapping1与mapping2交换位置即可。 Collections.sort(mappingList, new Comparator<Map.Entry<String,Integer>>(){   public int compare(Map.Entry<String,Integer> mapping1,Map.Entry<String,Integer> mapping2){   return mapping1.getValue().compareTo(mapping2.getValue());   } });}



【说明】

以上代码针对Java语言,传入的参数为待排序的哈希表,程序通过Collections.sort对该哈希表进行排序。