java map的重复键值的排序http://tbwuming.iteye.com/blog/1873634

1. 概述

    在java map中,一般有重复的key是使用IdentityHashMap存储,可以自动按键进行排序的是TreeMap,但是TreeMap不能使用重复的键,并且不能按照值进行排序,这就需要我们自己定义排序规则进行处理,下面记一下我做的重复键值排序,留个备份

 

2. 代码

Java代码   收藏代码
  1. package com.taobao.tpif.common;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.IdentityHashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9. import java.util.Map.Entry;  
  10.   
  11. import com.taobao.tpif.common.KeyValueComparator.Order;  
  12. import com.taobao.tpif.common.KeyValueComparator.Type;  
  13.   
  14. public class MapSorter {  
  15.   
  16.     public static void main(String[] args) {  
  17.         IdentityHashMap<Double, String> map = new IdentityHashMap<Double, String>();  
  18.         map.put(10.1"aaaa");  
  19.         map.put(10.1"bbbb");  
  20.         map.put(10.1"cccc");  
  21.         map.put(10.08"dddd");  
  22.         map.put(10.02"eeee");  
  23.         map.put(10.08"aaaa");  
  24.   
  25.         System.out.println("----------[ [ [ 排序前  ] ] ]----------\n");  
  26.   
  27.         for (Entry<Double, String> entry : map.entrySet()) {  
  28.             System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());  
  29.         }  
  30.   
  31.         System.out.println("\n----------[ [ [ 按键降序 ] ] ]----------\n");  
  32.   
  33.         List<Map.Entry<Double, String>> list1 = new ArrayList<Map.Entry<Double, String>>(map.entrySet());  
  34.         Collections.sort(list1, new KeyValueComparator(Type.KEY, Order.DESC));  
  35.   
  36.         for (Entry<Double, String> entry : list1) {  
  37.             System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());  
  38.         }  
  39.           
  40.         System.out.println("\n----------[ [ [ 按值升序  ] ] ]----------\n");  
  41.           
  42.         List<Map.Entry<Double, String>> list2 = new ArrayList<Map.Entry<Double, String>>(map.entrySet());  
  43.         Collections.sort(list2, new KeyValueComparator(Type.VALUE, Order.ASC));  
  44.   
  45.         for (Entry<Double, String> entry : list2) {  
  46.             System.out.println("\t" + entry.getKey() + "\t\t" + entry.getValue());  
  47.         }  
  48.   
  49.         System.out.println("\n----------[ [ [ 结束啦 ] ] ]----------\n");  
  50.     }  
  51. }  
  52.   
  53. class KeyValueComparator implements Comparator<Map.Entry<Double, String>> {  
  54.     enum Type {  
  55.         KEY, VALUE;  
  56.     }  
  57.   
  58.     enum Order {  
  59.         ASC, DESC  
  60.     }  
  61.   
  62.     private Type type;  
  63.     private Order order;  
  64.   
  65.     public KeyValueComparator(Type type, Order order) {  
  66.         this.type = type;  
  67.         this.order = order;  
  68.     }  
  69.   
  70.     @Override  
  71.     public int compare(Entry<Double, String> o1, Entry<Double, String> o2) {  
  72.         switch (type) {  
  73.         case KEY:  
  74.             switch (order) {  
  75.             case ASC:  
  76.                 return o1.getKey().compareTo(o2.getKey());  
  77.             case DESC:  
  78.                 return o2.getKey().compareTo(o1.getKey());  
  79.             default:  
  80.                 throw new RuntimeException("顺序参数错误");  
  81.             }  
  82.         case VALUE:  
  83.             switch (order) {  
  84.             case ASC:  
  85.                 return o1.getValue().compareTo(o2.getValue());  
  86.             case DESC:  
  87.                 return o2.getValue().compareTo(o1.getValue());  
  88.             default:  
  89.                 throw new RuntimeException("顺序参数错误");  
  90.             }  
  91.         default:  
  92.             throw new RuntimeException("类型参数错误");  
  93.         }  
  94.     }  
  95.   
  96. }  

 

3. 输出

Java代码   收藏代码
  1. ----------[ [ [ 排序前  ] ] ]------------  
  2.   
  3.     10.08       aaaa  
  4.     10.1        cccc  
  5.     10.08       dddd  
  6.     10.1        aaaa  
  7.     10.02       eeee  
  8.     10.1        bbbb  
  9.   
  10. ----------[ [ [ 按键降序 ] ] ]----------  
  11.   
  12.     10.1        cccc  
  13.     10.1        aaaa  
  14.     10.1        bbbb  
  15.     10.08       aaaa  
  16.     10.08       dddd  
  17.     10.02       eeee  
  18.   
  19. ----------[ [ [ 按值升序  ] ] ]----------  
  20.   
  21.     10.08       aaaa  
  22.     10.1        aaaa  
  23.     10.1        bbbb  
  24.     10.1        cccc  
  25.     10.08       dddd  
  26.     10.02       eeee  
  27.   
  28. ----------[ [ [ 结束啦 ] ] ]------------  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值