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

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

Map的排序常分为两种情况,1、按key值排序;2、按value排序

   默认的情况下,TreeMap:是按key升序,进行排序的;LinkedHashMap:是按加入顺序进行排序的;HashMap:内部数值的顺序并不是以存放的先后顺序为主,而是以hash值的顺序为主,其次才是存放的先后顺序。

    1.我们先讨论按key值进行排序

我们先看一下这个Sorter类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public 
class 
Sorter {
    
public 
static 
Map sort(Map map) {
        
Map<Object, Object> mapVK = 
new 
TreeMap<Object, Object>(
            
new 
Comparator<Object>() {
                
public 
int 
compare(Object obj1, Object obj2) {
                    
String v1 = (String)obj1;
                    
String v2 = (String)obj2;
                    
int 
s = v2.compareTo(v1);
                    
return 
s;
                
}
            
}
        
);
        
Set col = map.keySet();
        
Iterator iter = col.iterator();
        
while 
(iter.hasNext()) {
            
String key = (String) iter.next();
            
Integer value = (Integer) map.get(key);
            
mapVK.put(key, value);
        
}
        
return 
mapVK;
    
}
     
}

最后给出一个例子:

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
public 
class 
SortHashMap {
    
public 
SortHashMap() {
    
}
    
public 
static 
void 
main(String[] args) {
        
Map<String, Integer> maps = 
new 
HashMap<String, Integer>();
        
maps.put(
"boy"
8
);
        
maps.put(
"cat"
7
);
        
maps.put(
"dog"
1
);
        
maps.put(
"apple"
5
);
        
//排序前的输出
        
Set set = maps.entrySet();
        
Iterator i = set.iterator();
        
while
(i.hasNext()){
            
Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next();
            
System.out.println(entry1.getKey() + 
"-------->" 
+ entry1.getValue());
        
}
        
System.out.println(
"----------------"
);
         
//排序后的输出
        
Map<String, Integer> sortMaps = Sorter.sort(maps);
        
Set sortSet = sortMaps.entrySet();
        
Iterator ii = sortSet.iterator();
        
while
(ii.hasNext()){
            
Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)ii.next();
            
System.out.println(entry1.getKey() + 
"-------->" 
+ entry1.getValue());
        
}
         
    
}
}

排序前的输出结果是:

cat-------->7
apple-------->5
dog-------->1
boy-------->8

排序后的输出结果是:

dog-------->1
cat-------->7
boy-------->8
apple-------->5
经过排序后的Map有序了,是按照字母的逆序排列的。

2、我们再讨论如何按value值进行排序。

还是上面的那个例子,我们想要按照各类对象的数量打印出类别的名称。
我们再来修改一下这个SortHashMap类:

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
30
31
public 
class 
SortHashMap {
    
public 
SortHashMap() {
    
}
    
public 
static 
void 
main(String[] args) {
        
Map<String, Integer> maps = 
new 
HashMap<String, Integer>();
        
maps.put(
"boy"
8
);
        
maps.put(
"cat"
7
);
        
maps.put(
"dog"
1
);
        
maps.put(
"apple"
5
);
        
//排序前的输出
        
Set set = maps.entrySet();
        
Iterator i = set.iterator();
        
while
(i.hasNext()){
            
Map.Entry<String, Integer> entry1=(Map.Entry<String, Integer>)i.next();
            
System.out.println(entry1.getKey() + 
"-------->" 
+ entry1.getValue());
        
}
        
System.out.println(
"----------------"
);
        
//排序后的输出
     
List<Map.Entry<String, Integer>> info = 
                
new 
ArrayList<Map.Entry<String, Integer>>(maps.entrySet());
        
Collections.sort(info, 
new 
Comparator<Map.Entry<String, Integer>>() {
            
public 
int 
compare(Map.Entry<String, Integer> obj1, 
            
Map.Entry<String, Integer> obj2) {
                
return 
obj2.getValue() - obj1.getValue();
            
}
        
});
        
for 
(
int 
j = 
0
; j<info.size();j++) {
            
System.out.println(info.get(j).getKey() + 
"------->" 
+ info.get(j).getValue());
        
}
    
}
}

排序前的输出结果是:

cat-------->7
apple-------->5
dog-------->1
boy-------->8

排序后的输出结果是:

boy------->8
cat------->7
apple------->5
dog------->1

程序运行的结果,达到了我们的要求,实现了Map的排序。该方法主要是利用了ArrayList的排序实现了Map的排序输出,并没有影响到Map的存放结构。

特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp42
你可能感兴趣的文章
分组聚合
查看>>
冒泡排序(bubble sort)
查看>>
eclipse新建JSP页面报错:Multiple annotations found at this line解决方法
查看>>
bzoj3685
查看>>
为什么更喜欢Outlook,而不是Gmail
查看>>
C#中的事件和委托
查看>>
设计模式之(Adapter)适配器模式
查看>>
python中函数的定义、返回值以及参数的简要介绍
查看>>
没有名字
查看>>
4/16 近期状态
查看>>
线程基础2
查看>>
【本周主题】第二期:浏览器组成及工作原理深度了解
查看>>
Unity Webplayer installation error- Unity Webplayer update finished, but installed..
查看>>
自定义美化滚动条
查看>>
idea之查看类的上下级继承关系
查看>>
preHandle 添加参数和重写参数的问题,重写HttpServletRequestWrapper和Filter
查看>>
〖Android〗依据资源信息,Mock Android资源
查看>>
Spring(一)容器
查看>>
给ASP.net程序配置代理服务器
查看>>
Java探索之旅(7)——对象的思考
查看>>