Map.Entry

本文详细介绍了如何使用Java对Map中的键(key)和值(value)进行排序。通过TreeMap和Comparator可以实现键的升序或降序排列,而值的排序则可以通过将Map转换为List再进行排序的方法来实现。

Map.Entry

  Map是java中的接口,Map.Entry是Map的一个内部接口。

  Map提供了一些常用方法,如keySet()、entrySet()等方法,keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。

  Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法。

  是在刷leedcode题学习用了下Map.Entry  223. Rectangle Area,我是用HashMap存入的输入信息的,下面就遇到了要对map的value排序的问题了

 

首先,如果要对map的key排序的话

  如果是升序,那么TreeMap就可以解决了,因为TreeMap默认的就是升序,但是我们也可以使用Comparator来改变它的排序的方式。

 1 package Map;
 2 
 3 import java.util.Comparator;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Set;
 7 import java.util.TreeMap;
 8 
 9 public class TreeMapTest {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         
14         Map<Integer,Integer> map=new TreeMap<Integer,Integer>(
15                 new Comparator<Integer>(){
16                     public int compare(Integer obj1,Integer obj2){
17                         return obj1-obj2;//控制升序还是降序
18                         //如果String则obj1.compareTo(obj2)
19                     }
20                 });
21         map.put(3, 1);
22         map.put(1, 1);
23         map.put(5, 1);
24         map.put(4, 1);
25         map.put(2, 1);
26         
27         Set<Integer> keySet=map.keySet();
28         Iterator<Integer> iter=keySet.iterator();
29         while(iter.hasNext()){
30             Integer key=iter.next();
31             System.out.println(key+":"+map.get(key));
32         }    
33     }
34 }
View Code

然后,如果是对map的value排序的话

 1 package Map;
 2 
 3 
 4 import java.util.ArrayList;
 5 import java.util.Collections;
 6 import java.util.Comparator;
 7 import java.util.HashMap;
 8 import java.util.List;
 9 import java.util.Map;
10 import java.util.Map.Entry;
11 
12 public class HashMapTest {
13 
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16         Map<Integer,Integer> map=new HashMap<Integer,Integer>();
17         map.put(1, 3);
18         map.put(3, 5);
19         map.put(5, 1);
20         map.put(4, 2);
21         map.put(2, 4);
22         List<Map.Entry<Integer,Integer>> list=new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
23         Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){
24             public int compare(Entry<Integer,Integer>obj1,Entry<Integer,Integer>obj2){
25                 return obj1.getValue()-obj2.getValue();
26             }
27         });
28         for (Map.Entry<Integer, Integer>mapp:list){
29             System.out.println(mapp.getKey()+":"+mapp.getValue());
30             
31         }
32     }
33 }
View Code

其中 Lambda表达式之比较器参考了https://my.oschina.net/biezhi/blog/506433

转载于:https://www.cnblogs.com/luluqiao/p/5921780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值