博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Map的排序
阅读量:7236 次
发布时间:2019-06-29

本文共 4537 字,大约阅读时间需要 15 分钟。

1.使用TreeMap<K,V>实现对Map<K,V>排序

TreeMap中,是实现了对key的排序,不能根据value的大小来排序,其中的K,必须实现一个可比较的接口。

1. TreepMap通过传入比较器的构造方法

public TreeMap(Comparator<? super K> comparator) {

this.comparator = comparator;

}

 

2.也可以通过comparator()方法传入

public Comparator
comparator() {
return comparator;
}

 

3.在向TreeMapput元素时实现了根据key来排序

510      public V put(K key, V value) {
        …………
525          // split comparator and comparable paths
526          Comparator
cpr = comparator;
527          if (cpr != null) {
528              do {
529                  parent = t;
530                  cmp = cpr.compare(key, t.key);
531                  if (cmp < 0)
532                      t = t.left;
533                  else if (cmp > 0)
534                      t = t.right;
535                  else
536                      return t.setValue(value);
537              } while (t != null);
538          }
539          else {
540              if (key == null)
541                  throw new NullPointerException();
542              Comparable
k = (Comparable
) key;
543              do {
544                  parent = t;
545                  cmp = k.compareTo(t.key);
546                  if (cmp < 0)
547                      t = t.left;
548                  else if (cmp > 0)
549                      t = t.right;
550                  else
551                      return t.setValue(value);
552              } while (t != null);
553          }
        …………
563      }

 

 

2.根据Value值对Map排序

同样Map<K,V>中的V也发现可比较,即实现了comparable接口,示例如下:

1.实现了comparable接口的对象

11   public class Music implements Comparable
,Serializable{
12    …………
128      public int compareTo(Music o) {
129          int i = o.getPeakTime().compareTo(this.getPeakTime()); //倒序
130          if(i==0){
131              i = o.getNum().compareTo(this.getNum());
132              if(i == 0){
133                  i = o.getNeid().compareTo(this.getNeid());
134              }
135          }
136          return i;
137      }
138   …………
169  }

 

2.排序

 

3.排序的方法

14   public class MapUtil {
15
16      						…………
24       public static 
> Map
sortByValue( Map
map ){
25           List
> list =
26                   new LinkedList
>( map.entrySet() );
27           Collections.sort(list, new Comparator
>() {
28               public int compare(Map.Entry
o1, Map.Entry
o2) {
29                   int res = o1.getValue().compareTo(o2.getValue());
30                   return res != 0 ? res : 1; // 处理当value相等时,不丢失元素
31               }
32           });
33           Map
result = new LinkedHashMap
();
34           for (Map.Entry
entry : list){
35               result.put( entry.getKey(), entry.getValue() );
36           }
37           return result;
38       }
39    …………
63       public static 
> SortedSet
> entriesSortedByValues(Map
map) {
64           SortedSet
> sortedEntries = new TreeSet
>(
65                   new Comparator
>() {
66                       @Override public int compare(Map.Entry
e1, Map.Entry
e2) {
67                           int res = e1.getValue().compareTo(e2.getValue());
68                           return res != 0 ? res : 1; // Special fix to preserve items with equal values
69                       }
70                   }
71           );
72           sortedEntries.addAll(map.entrySet());
73           return sortedEntries;
74       }
75    …………
76       public static void main(String[] args){
77           Map
nonSortedMap = new HashMap
();
78            …………
96
97   //        nonSortedMap.put("aaa", 1);
98   //        nonSortedMap.put("ddd", 3);
99   //        nonSortedMap.put("ccc", 1);
100  //        nonSortedMap.put("rrrr", 2);
101
102          Map
sortedMap = MapUtil.sortByValue(nonSortedMap);
103          for(Map.Entry
entry : sortedMap.entrySet()){
104              System.out.println(entry.getKey()+":"+entry.getValue());
105          }
106  //        for (Map.Entry
entry : entriesSortedByValues(nonSortedMap)) {
107  //            System.out.println(entry.getKey()+":"+entry.getValue());
108  //        }
109      }
110  }

 

 

 

转载于:https://www.cnblogs.com/luowei010101/archive/2012/07/15/2592351.html

你可能感兴趣的文章
UGUI之Canvas和EventSystem
查看>>
atitit.web 推送实现方案集合
查看>>
java中byte, iso-8859-1, UTF-8,乱码的根源
查看>>
git web 服务器的搭建【转】
查看>>
初窥ElasticSearch
查看>>
identity_insert---实验性插入大批量数据和分页存储过程
查看>>
键盘游戏之canvas--用OO方式写
查看>>
Leetcode: Scramble String
查看>>
JavaWeb--中文乱码小结
查看>>
MySQL优化经验和方法汇总
查看>>
JAVA获取CLASSPATH路径--转
查看>>
Linux 下测试网卡性能命令iperf 的用法
查看>>
Qt编写自定义控件大全
查看>>
.yml是什么文件
查看>>
工作总结 datatable 里的 数据 rows Columns
查看>>
006-生命周期和插件
查看>>
Geometry Imager Viewport Filter
查看>>
mysql 批量kill locked 进程
查看>>
Unix 环境高级编程
查看>>
WinForm(VB.NET)获取当前登录的用户名
查看>>