---
title: "Map 排序"
author: "grefr"
date: "2013-08-14T17:32:40Z"
tags: []
categories: ["编程语言"]
slug: "map"
draft: false
lastmod: "2015-06-18T11:21:19Z"
id: 1924583
url: "https://grefr.iteye.com/blog/1924583"
---

详见：<http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp42>

Map的排序常分为两种情况，1、按key值排序；2、按value排序

    1.我们先讨论按key值进行排序
我们先看一下这个Sorter类：

    public static Map sort(Map map) {
         Map mapVK = new TreeMap(
             new Comparator() {
                 public int compare(Object obj1, Object obj2) {
                     String v1 = (String)obj1;
                     String v2 = (String)obj2;
                     int s = v2.compareTo(v1);
                     return s;
                 }
             }
         );
        Set col = map.keySet();
         Iterator iter = col.iterator();
         while (iter.hasNext()) {
             String key = (String) iter.next();
             Integer value = (Integer) map.get(key);
             mapVK.put(key, value);
         }
         return mapVK;
     }
     
 }

 public class SortHashMap {

    }
    public static void main(String args) {
         Map maps = new HashMap();
         maps.put(&quot;boy&quot;, 8);
         maps.put(&quot;cat&quot;, 7);
         maps.put(&quot;dog&quot;, 1);
         maps.put(&quot;apple&quot;, 5);
         //排序前的输出
         Set set = maps.entrySet();
         Iterator i = set.iterator();
         while(i.hasNext()){
             Map.Entry entry1=(Map.Entry)i.next();
             System.out.println(entry1.getKey() + &quot;--------&gt;&quot; + entry1.getValue());
         }
        System.out.println(&quot;----------------&quot;);
          //排序后的输出
         Map sortMaps = Sorter.sort(maps);
         Set sortSet = sortMaps.entrySet();
         Iterator ii = sortSet.iterator();
         while(ii.hasNext()){
             Map.Entry entry1=(Map.Entry)ii.next();
             System.out.println(entry1.getKey() + &quot;--------&gt;&quot; + entry1.getValue());
         }
         
     }
 }

 cat--------&gt;7
 apple--------&gt;5
 dog--------&gt;1
 boy--------&gt;8

 dog--------&gt;1
 cat--------&gt;7
 boy--------&gt;8
 apple--------&gt;5
经过排序后的Map有序了，是按照字母的逆序排列的。

还是上面的那个例子，我们想要按照各类对象的数量打印出类别的名称。
我们再来修改一下这个SortHashMap类：

    public SortHashMap() {

    public static void main(String args) {
         Map maps = new HashMap();
         maps.put(&quot;boy&quot;, 8);
         maps.put(&quot;cat&quot;, 7);
         maps.put(&quot;dog&quot;, 1);
         maps.put(&quot;apple&quot;, 5);
         //排序前的输出
         Set set = maps.entrySet();
         Iterator i = set.iterator();
         while(i.hasNext()){
             Map.Entry entry1=(Map.Entry)i.next();
             System.out.println(entry1.getKey() + &quot;--------&gt;&quot; + entry1.getValue());
         }
        System.out.println(&quot;----------------&quot;);
         //排序后的输出
      List&gt; info = new ArrayList&gt;(maps.entrySet());
         Collections.sort(info, new Comparator&gt;() {
             public int compare(Map.Entry obj1, Map.Entry obj2) {
                 return obj2.getValue() - obj1.getValue();
             }
         });
        for (int j = 0; j&quot; + info.get(j).getValue());
         }
     }
 }

 cat--------&gt;7
 apple--------&gt;5
 dog--------&gt;1
 boy--------&gt;8

 boy-------&gt;8
 cat-------&gt;7
 apple-------&gt;5
 dog-------&gt;1