Map.Entry
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来改变它的排序的方式。
package Map; import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap; public class TreeMapTest { public static void main(String[] args) {
// TODO Auto-generated method stub Map<Integer,Integer> map=new TreeMap<Integer,Integer>(
new Comparator<Integer>(){
public int compare(Integer obj1,Integer obj2){
return obj1-obj2;//控制升序还是降序
//如果String则obj1.compareTo(obj2)
}
});
map.put(3, 1);
map.put(1, 1);
map.put(5, 1);
map.put(4, 1);
map.put(2, 1); Set<Integer> keySet=map.keySet();
Iterator<Integer> iter=keySet.iterator();
while(iter.hasNext()){
Integer key=iter.next();
System.out.println(key+":"+map.get(key));
}
}
}
然后,如果是对map的value排序的话
package Map; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; public class HashMapTest { public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
map.put(1, 3);
map.put(3, 5);
map.put(5, 1);
map.put(4, 2);
map.put(2, 4);
List<Map.Entry<Integer,Integer>> list=new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){
public int compare(Entry<Integer,Integer>obj1,Entry<Integer,Integer>obj2){
return obj1.getValue()-obj2.getValue();
}
});
for (Map.Entry<Integer, Integer>mapp:list){
System.out.println(mapp.getKey()+":"+mapp.getValue()); }
}
}
其中 Lambda表达式之比较器参考了https://my.oschina.net/biezhi/blog/506433
Map.Entry的更多相关文章
- Map接口,Map.Entry,hashMap类,TreeMap类,WeakHashMap。
Collection接口之前接触过,每次保存的对象是一个对象,但是在map中保存的是一对对象,是以key->value形式保存的. 定义: public interface Map<K,V ...
- java的Map及Map.Entry解析
Map<K,V>是以键-值对存储的(key-value), 而Entry<K,V>是Map中的一个接口,Map.Entry<K,V>接口主要用于获取.比较 key和 ...
- java Map及Map.Entry详解
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...
- Map.Entry用法示例
一般在HashMap中可以通过key值得到value值,以key作为检索项.Map.Entry<K,V>可以作为条目的检索项.HashMap中有entrySet()方法,返回值是Set&l ...
- 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()
源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这 ...
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced。。.相似的错误
这个问题是出现一般都是因为JDK版本的问题.今天公司安装NC的时候就出现了这个问题.经过对错误的分析和猜测,将JDK从1.8i换成了1.7,之后就行了.根据我个人的猜测,可能是1.8以后就不支持Map ...
- Java—Map.Entry
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...
- java Map及Map.Entry详解(转)
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...
- 介绍map.entry接口
Map是java中的接口,Map.Entry是Map的一个内部接口.java.util.Map.Entry接口主要就是在遍历map的时候用到. Map提供了一些常用方法,如keySet().entry ...
随机推荐
- Odoo中的Javascript单元测试
前端页面利用QUnit进行单元测试,本文参考官方文档:https://www.odoo.com/documentation/8.0/reference/javascript.html 访问/web/t ...
- HTTPS协议简介
一.HTTPS简介 百度已经于近日上线了全站 HTTPS 的安全搜索,默认会将 HTTP 请求跳转成 HTTPS.本文重点介绍 HTTPS 协议, 并简单介绍部署全站 HTTPS 的意义. HTTPS ...
- 【HDU3861 强连通分量缩点+二分图最小路径覆盖】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3861 题目大意:一个有向图,让你按规则划分区域,要求划分的区域数最少. 规则如下:1.有边u到v以及有 ...
- oracle 字符乱码问题解决方案
今天在客户服务器上遇到了oracle中文乱码问题,第一个想到的是:要想避免oracle字符乱码的问题,需要注意oracle客户端的字符编码和服务端的字符编码保持一致. 于是操作如下: 1.查看服务端字 ...
- redis 安装(centos 6.4)
我使用6.4系统,mark一下,其他版本应该也一样. wget wget http://download.redis.io/releases/redis-3.0.6.tar.gz make make ...
- what's the difference between dim as and dim as new?
what's the difference between dim as and dim as new? There is no difference with value types (Intege ...
- IIS删除http header信息如Server, X-Powered-By, 和X-AspNet-Version
响应头信息原始头信息 Cache-Control private Content-Length 78457 Content-Type text/html; charset=utf-8 Date Fri ...
- 《Linux内核分析》第七周 可执行程序的装载
[刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK SEVEN ...
- 【爬虫】python之BeautifulSoup用法
1.爬虫 网络爬虫是捜索引擎抓取系统的重要组成部分.爬虫的主要目的是将互联网上的网页下载到本地形成一个或联网内容的镜像备份.通过分析和过滤HTML 代码,实现对图片.文字等资源的获取. 2.pytho ...
- 统一的mvc异常处理
mvc异常处理 using System; using System.Configuration; using System.Web.Mvc; using Infrastructure.Excepti ...