免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1397 | 回复: 0
打印 上一主题 下一主题

对map key 和Value 排序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-13 11:05 |只看该作者 |倒序浏览
对MAP KEY排序
  1. package study.base.map;

  2. import java.util.Comparator;
  3. import java.util.Map;
  4. import java.util.TreeMap;

  5. public class SortKeyMap {
  6.     public static void main(String[] args) {
  7.         SortKeyMap sortMap=new SortKeyMap();
  8.         Map<String, Integer> map = new TreeMap<String, Integer>();
  9.         map.put("KFC",123456);
  10.         map.put("WNBA", 4566);
  11.         map.put("NBA", 333);
  12.         map.put("CBA", 44444);
  13.         Map<String, Integer> resultMap = sortMap.sortMapByValue(map); //按Value进行排序
  14.         for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
  15.             System.out.println(entry.getKey() + " " + entry.getValue());
  16.         }
  17.     }
  18.      
  19.     /**
  20.      * 使用 Map按value进行排序
  21.      * @param map
  22.      * @return
  23.      */
  24.     public Map<String, Integer> sortMapByValue(Map<String, Integer> map) {
  25.         if (map == null || map.isEmpty()) {
  26.             return null;
  27.         }
  28.         Map<String, Integer> sortMap = new TreeMap<String, Integer>(new MapComparator());  
  29.         sortMap.putAll(map);
  30.         return sortMap;
  31.     }
  32.      
  33.     //比较器类
  34.     class MapComparator implements Comparator<String> {
  35.                  
  36.         public int compare(String me1, String me2) {
  37.             return me1.compareTo(me2);
  38.         }
  39.     }
  40. }
复制代码
对MAP VALUE 排序
  1. package study.base.map;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.Iterator;
  6. import java.util.LinkedHashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import java.util.TreeMap;

  11. public class SortValueMap {
  12.     public static void main(String[] args) {
  13.         SortValueMap sortMap=new SortValueMap();
  14.         Map<String, Integer> map = new TreeMap<String, Integer>();
  15.         map.put("KFC",123456);
  16.         map.put("WNBA", 4566);
  17.         map.put("NBA", 333);
  18.         map.put("CBA", 44444);
  19.         Map<String, Integer> resultMap = sortMap.sortMapByValue(map); //按Value进行排序
  20.         for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
  21.             System.out.println(entry.getKey() + " " + entry.getValue());
  22.         }
  23.     }
  24.      
  25.     /**
  26.      * 使用 Map按value进行排序
  27.      * @param map
  28.      * @return
  29.      */
  30.     public Map<String, Integer> sortMapByValue(Map<String, Integer> map) {
  31.         if (map == null || map.isEmpty()) {
  32.             return null;
  33.         }
  34.          
  35.         Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
  36.         List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
  37.         Collections.sort(entryList, new MapValueComparator());
  38.         Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
  39.         Map.Entry<String, Integer> tmpEntry = null;
  40.         while (iter.hasNext()) {
  41.             tmpEntry = iter.next();
  42.             sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
  43.         }
  44.         return sortedMap;
  45.     }
  46.      
  47.     //比较器类
  48.     class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
  49.                  
  50.         public int compare(Entry<String, Integer> me1, Entry<String, Integer> me2) {
  51.             return me1.getValue().compareTo(me2.getValue());
  52.         }
  53.     }
  54. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP