Map是鍵值對(duì)的集合接口,它的實(shí)現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。
•TreeMap:基于紅黑樹(shù)(Red-Black tree)的 NavigableMap 實(shí)現(xiàn),該映射根據(jù)其鍵的自然順序進(jìn)行排序,或者根據(jù)創(chuàng)建映射時(shí)提供的 Comparator 進(jìn)行排序,具體取決于使用的構(gòu)造方法。
•HashMap的值是沒(méi)有順序的,它是按照key的HashCode來(lái)實(shí)現(xiàn)的,對(duì)于這個(gè)無(wú)序的HashMap我們要怎么來(lái)實(shí)現(xiàn)排序呢?參照TreeMap的value排序。
Map.Entry返回Collections視圖。
按key排序
TreeMap默認(rèn)是升序的,如果我們需要改變排序方式,則需要使用比較器:Comparator。Comparator可以對(duì)集合對(duì)象或者數(shù)組進(jìn)行排序的比較器接口,實(shí)現(xiàn)該接口的public compare(T o1,To2)方法即可實(shí)現(xiàn)排序。
注意:以下代碼均已在Jdk1.6測(cè)試通過(guò)了
TreeMap默認(rèn)按key升序排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void keyUpSort() { // 默認(rèn)情況,TreeMap按key升序排序 Map<String, Integer> map = new TreeMap<String, Integer>(); map.put( "acb1" , 5 ); map.put( "bac1" , 3 ); map.put( "bca1" , 20 ); map.put( "cab1" , 80 ); map.put( "cba1" , 1 ); map.put( "abc1" , 10 ); map.put( "abc2" , 12 ); // 默認(rèn)情況下,TreeMap對(duì)key進(jìn)行升序排序 System.out.println( "------------正常情況,TreeMap按key升序排序--------------------" ); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } |
修改TreeMap的排序方式,按key降序排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static void keyDownSort() { // TreeMap,按key降序排序 // 降序排序比較器 Comparator<String> keyComparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { // TODO Auto-generated method stub return o2.compareTo(o1); } }; Map<String, Integer> map = new TreeMap<String, Integer>(keyComparator); map.put( "acb1" , 5 ); map.put( "bac1" , 3 ); map.put( "bca1" , 20 ); map.put( "cab1" , 80 ); map.put( "cba1" , 1 ); map.put( "abc1" , 10 ); map.put( "abc2" , 12 ); System.out.println( "------------TreeMap按key降序排序--------------------" ); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } |
按Value排序
以下只演示按TreeMap按Value升序排序,這同樣適用于HashMap。
修改TreeMap的排序方式,按Value升序排序
注意:正常情況下Map是不可以使用Collections.sort()方法進(jìn)行排序的,不過(guò)可以將Map轉(zhuǎn)換成list之后再進(jìn)行排序。
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
|
public static void valueUpSort() { // 默認(rèn)情況,TreeMap按key升序排序 Map<String, Integer> map = new TreeMap<String, Integer>(); map.put( "acb1" , 5 ); map.put( "bac1" , 3 ); map.put( "bca1" , 20 ); map.put( "cab1" , 80 ); map.put( "cba1" , 1 ); map.put( "abc1" , 10 ); map.put( "abc2" , 12 ); // 升序比較器 Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String,Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { // TODO Auto-generated method stub return o1.getValue()-o2.getValue(); } }; // map轉(zhuǎn)換成list進(jìn)行排序 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); // 排序 Collections.sort(list,valueComparator); // 默認(rèn)情況下,TreeMap對(duì)key進(jìn)行升序排序 System.out.println( "------------map按照value升序排序--------------------" ); for (Map.Entry<String, Integer> entry : list) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } |
測(cè)試結(jié)果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
------------正常情況,TreeMap按key升序排序-------------------- abc1: 10 abc2: 12 acb1: 5 bac1: 3 bca1: 20 cab1: 80 cba1: 1 ------------TreeMap按key降序排序-------------------- cba1: 1 cab1: 80 bca1: 20 bac1: 3 acb1: 5 abc2: 12 abc1: 10 ------------map按照value升序排序-------------------- cba1: 1 bac1: 3 acb1: 5 abc1: 10 abc2: 12 bca1: 20 cab1: 80 |
以上所述是小編給大家介紹的Java Map 按照Value排序的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/VioletLove/archive/2016/08/15/5772627.html