Map 按key和value 排序

Map的排序常分为两种情况,1、按key值排序;2、按value排序

   默认的情况下,TreeMap:是按key升序,进行排序的;LinkedHashMap:是按加入顺序进行排序的;HashMap:内部数值的顺序并不是以存放的先后顺序为主,而是以hash值的顺序为主,其次才是存放的先后顺序。

    1.我们先讨论按key值进行排序
我们先看一下这个Sorter类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public  class  Sorter {
     public  static  Map sort(Map map) {
         Map<Object, Object> mapVK =  new  TreeMap<Object, Object>(
             new  Comparator<Object>() {
                 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;
     }
     
}

最后给出一个例子:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public  class  SortHashMap {
     public  SortHashMap() {
     }
     public  static  void  main(String[] args) {
         Map<String, Integer> maps =  new  HashMap<String, Integer>();
         maps.put( "boy" 8 );
         maps.put( "cat" 7 );
         maps.put( "dog" 1 );
         maps.put( "apple" 5 );
         //排序前的输出
         Set set = maps.entrySet();
         Iterator i = set.iterator();
         while (i.hasNext()){
             Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next();
             System.out.println(entry1.getKey() +  "-------->"  + entry1.getValue());
         }
         System.out.println( "----------------" );
          //排序后的输出
         Map<String, Integer> sortMaps = Sorter.sort(maps);
         Set sortSet = sortMaps.entrySet();
         Iterator ii = sortSet.iterator();
         while (ii.hasNext()){
             Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)ii.next();
             System.out.println(entry1.getKey() +  "-------->"  + entry1.getValue());
         }
         
     }
}


排序前的输出结果是:
cat-------->7
apple-------->5
dog-------->1
boy-------->8

排序后的输出结果是:
dog-------->1
cat-------->7
boy-------->8
apple-------->5
经过排序后的Map有序了,是按照字母的逆序排列的。

2、我们再讨论如何按value值进行排序。
还是上面的那个例子,我们想要按照各类对象的数量打印出类别的名称。
我们再来修改一下这个SortHashMap类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public  class  SortHashMap {
     public  SortHashMap() {
     }
     public  static  void  main(String[] args) {
         Map<String, Integer> maps =  new  HashMap<String, Integer>();
         maps.put( "boy" 8 );
         maps.put( "cat" 7 );
         maps.put( "dog" 1 );
         maps.put( "apple" 5 );
         //排序前的输出
         Set set = maps.entrySet();
         Iterator i = set.iterator();
         while (i.hasNext()){
             Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next();
             System.out.println(entry1.getKey() +  "-------->"  + entry1.getValue());
         }
         System.out.println( "----------------" );
         //排序后的输出
      List<Map.Entry<String, Integer>> info = 
                 new  ArrayList<Map.Entry<String, Integer>>(maps.entrySet());
         Collections.sort(info,  new  Comparator<Map.Entry<String, Integer>>() {
             public  int  compare(Map.Entry<String, Integer> obj1, 
             Map.Entry<String, Integer> obj2) {
                 return  obj2.getValue() - obj1.getValue();
             }
         });
         for  ( int  j =  0 ; j<info.size();j++) {
             System.out.println(info.get(j).getKey() +  "------->"  + info.get(j).getValue());
         }
     }
}

排序前的输出结果是:
cat-------->7
apple-------->5
dog-------->1
boy-------->8

排序后的输出结果是:
boy------->8
cat------->7
apple------->5
dog------->1

程序运行的结果,达到了我们的要求,实现了Map的排序。该方法主要是利用了ArrayList的排序实现了Map的排序输出,并没有影响到Map的存放结构。


特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp42
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值