package test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.Map.Entry; public class MapSortDemo { /** * @param args */ public static void main(String[] args) { Map<String, String> hMap = new HashMap<String, String>(); hMap.put("a", "3"); hMap.put("z", "2"); hMap.put("b", "6"); hMap.put("o", "9"); System.out.println("根据key升序排序"); Map<String, String> sortByKeyResultMap = sortMapByKey(hMap); //按Key进行排序 Iterator<Map.Entry<String, String>> sortByKeyEntries = sortByKeyResultMap.entrySet().iterator(); while (sortByKeyEntries.hasNext()) { Map.Entry<String, String> entry = sortByKeyEntries.next(); System.out.println("Key = " + entry.getKey() + "------->Value = " + entry.getValue()); } System.out.println("------------------------------"); System.out.println("根据value降序排序"); Map<String, String> sortByValueResultMap = sortMapByValue(hMap); //按Value进行排序 Iterator<Map.Entry<String, String>> sortByValueEntries = sortByValueResultMap.entrySet().iterator(); while (sortByValueEntries.hasNext()) { Map.Entry<String, String> entry = sortByValueEntries.next(); System.out.println("Key = " + entry.getKey() + "------->Value = " + entry.getValue()); } } /** * 使用 Map按key进行排序 * @param map * @return */ public static Map<String, String> sortMapByKey(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } // Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator()); Map<String, String> sortMap = new TreeMap<String, String>(new Comparator<String>() { public int compare(String obj1, String obj2) { return obj1.compareTo(obj2);//升序排序 } }); sortMap.putAll(map); return sortMap; } /** * 使用 Map按value进行排序 * @param map * @return */ public static Map<String, String> sortMapByValue(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } Map<String, String> sortedMap = new LinkedHashMap<String, String>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(map.entrySet()); // Collections.sort(entryList, new MapValueComparator()); Collections.sort( entryList, new Comparator<Map.Entry<String, String>>(){ public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o2.getValue().compareTo(o1.getValue());// 降序排序 } } ); Iterator<Map.Entry<String, String>> iter = entryList.iterator(); Map.Entry<String, String> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } return sortedMap; } }
根据key升序排序 Key = a------->Value = 3 Key = b------->Value = 6 Key = o------->Value = 9 Key = z------->Value = 2 ------------------------------ 根据value降序排序 Key = o------->Value = 9 Key = b------->Value = 6 Key = a------->Value = 3 Key = z------->Value = 2
如果想把map排序代码分离出来就添加两个类,分别为
package test; import java.util.Comparator; public class MapKeyComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }
package test; import java.util.Comparator; import java.util.Map; import java.util.Map.Entry; public class MapValueComparator implements Comparator<Map.Entry<String, String>> { @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o2.getValue().compareTo(o1.getValue()); } }
注意o1和o2的位置,它们在前还是在后就会变化升序和降序的顺序。
用key排序用以下代码
用value排序用以下代码