按比较器排序映射
//package com.nowjava; /** from * n o w j a v a . c o m **/ import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { /* from nowjava - 时 代 Java*/ public static Map<String, Integer> sortByComparator( Map<String, Integer> unsortMap, final boolean order) { List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>( unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map<