Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518534
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2012-03-06 19:39:49

RT

运行结果
  1. ========================== TEST 1 (sort by key)
  2. input = {83=0, 85=1, 33=2, 92=3, 57=4, 26=5, 56=6, 25=7, 22=8, 47=9}
  3. result = {22=8, 25=7, 26=5, 33=2, 47=9, 56=6, 57=4, 83=0, 85=1, 92=3}

  4. ========================== TEST 2 (sort by value)
  5. input = {0=83, 1=85, 2=33, 3=92, 4=57, 5=26, 6=56, 7=25, 8=22, 9=47}
  6. result = {8=22, 7=25, 5=26, 2=33, 9=47, 6=56, 4=57, 0=83, 1=85, 3=92}

TestSortMapByValue.java

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

  11. /**
  12.  * 将Map按照value进行排序。
  13.  *
  14.  * @author btpka3@163.com
  15.  */
  16. public class TestSortMapByValue {

  17.     /**
  18.      * @param args
  19.      */
  20.     public static void main(String[] args) {
  21.         test01();
  22.         test02();
  23.     }

  24.     /**
  25.      * 测试使用按照Key排序的Map。
  26.      *
  27.      * 使用JDK提供的TreeMap即可按照key进行排序。

  28.      * 使用JDK提供的LinkedHashMap即可保留插入时的先后顺序。
  29.      */
  30.     public static void test01() {
  31.         System.out.println("========================== TEST 1 (sort by key)");
  32.         Map<String, Integer> input = new LinkedHashMap<String, Integer>();

  33.         Random r = new Random(System.currentTimeMillis());
  34.         for (int i = 0; i < 10; i++) {
  35.             String k = Integer.toString(Math.abs(r.nextInt()) % 90 + 10);
  36.             input.put(k, i);
  37.         }
  38.         Map<String, Integer> output = new TreeMap<String, Integer>(input);

  39.         System.out.println("input = " + input);
  40.         System.out.println("result = " + output);
  41.         System.out.println();
  42.     }

  43.     /**
  44.      * 测试将Map按照Value排序。
  45.      */
  46.     public static void test02() {
  47.         System.out.println("========================== TEST 2 (sort by value)");
  48.         Map<String, Integer> input = new LinkedHashMap<String, Integer>();

  49.         Random r = new Random(System.currentTimeMillis());
  50.         for (int i = 0; i < 10; i++) {
  51.             int v = Math.abs(r.nextInt()) % 90 + 10;
  52.             input.put(i + "", v);
  53.         }
  54.         Map<String, Integer> output = sortByValue(input);
  55.         System.out.println("input = " + input);
  56.         System.out.println("result = " + output);
  57.         System.out.println();
  58.     }

  59.     // 参考:
  60.     public static <K, V extends Comparable<V>> Map<K, V> sortByValue(
  61.             Map<K, V> map) {
  62.         List<Entry<K, V>> list = new LinkedList<Entry<K, V>>(map.entrySet());
  63.         Collections.sort(list, new Comparator<Entry<K, V>>() {
  64.             public int compare(Entry<K, V> o1, Entry<K, V> o2) {

  65.                 Comparable<V> v1 = o1.getValue();
  66.                 V v2 = o2.getValue();
  67.                 if (v1 == null) {
  68.                     if (v2 == null) {
  69.                         return 0;
  70.                     } else {
  71.                         return -1;
  72.                     }
  73.                 } else {
  74.                     if (v2 == null) {
  75.                         return 1;
  76.                     } else {
  77.                         return v1.compareTo(v2);
  78.                     }
  79.                 }
  80.             }
  81.         });
  82.         Map<K, V> result = new LinkedHashMap<K, V>();
  83.         Iterator<Entry<K, V>> it = list.iterator();
  84.         while (it.hasNext()) {
  85.             Entry<K, V> entry = it.next();
  86.             result.put(entry.getKey(), entry.getValue());
  87.         }
  88.         return result;
  89.     }
  90. }




阅读(1309) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~