Map是鍵值對的集合接口,它的實現類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。
•TreeMap:基于紅黑樹(Red-Black tree)的 NavigableMap 實現,該映射根據其鍵的自然順序進行排序,或者根據創建映射時提供的 Comparator 進行排序,具體取決于使用的構造方法。
•HashMap的值是沒有順序的,它是按照key的HashCode來實現的,對于這個無序的HashMap我們要怎么來實現排序呢?參照TreeMap的value排序。
Map.Entry返回Collections視圖。
按key排序
TreeMap默認是升序的,如果我們需要改變排序方式,則需要使用比較器:Comparator。Comparator可以對集合對象或者數組進行排序的比較器接口,實現該接口的public compare(T o1,To2)方法即可實現排序。
注意:以下代碼均已在Jdk1.6測試通過了
TreeMap默認按key升序排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void keyUpSort() { // 默認情況,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 ); // 默認情況下,TreeMap對key進行升序排序 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()方法進行排序的,不過可以將Map轉換成list之后再進行排序。
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() { // 默認情況,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轉換成list進行排序 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); // 排序 Collections.sort(list,valueComparator); // 默認情況下,TreeMap對key進行升序排序 System.out.println( "------------map按照value升序排序--------------------" ); for (Map.Entry<String, Integer> entry : list) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } |
測試結果
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排序的實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/VioletLove/archive/2016/08/15/5772627.html