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 ...
随机推荐
- 使用explain查看mysql查询执行计划
explain语句: 字段解释: type: all(全表扫描) ref() possible_keys: 预测使用什么列做为索引 key: 实际使用的key ...
- unity3d插件Daikon Forge GUI 中文教程6-高级控件richtextlabel的使用
3.5.richtextlabel文本 可以像Word文档一样编辑出多样的内容,图片,字体颜色大小下划线.超链接背景等等. Defaults: 默认字体 默认图集 Blank Texture :空白的 ...
- daterangepicker 日期范围插件自定义 可选 年份
minDate:'01/01/2012',maxDate:'01/01/2015' $("#txtPODate").daterangepicker({ singleDatePick ...
- 对ASM存储管理的一些初步理解记录
ASM:Automatic Storage Management,是ORACEL10G以后为了简化存储管理的复杂性,也是为了摆脱对其他厂商的依赖而推出的.ASM作为目前ORACLE推荐的首选存储方案, ...
- Struts和SpringMVC两种MVC框架比较
基于Web的MVC framework在J2EE的世界内已是空前繁荣.TTS网站上几乎每隔一两个星期就会有新的MVC框架发布.目前比较好的MVC,老牌的有Struts.Webwork.新兴的MVC框架 ...
- HttpClientUtils.java
package com.vcredit.ddcash.batch.util; import java.io.BufferedReader;import java.io.ByteArrayOutputS ...
- 对C++对象实例化的测试
#include <iostream> using namespace std; class class1 { public: class1(){ } class1(int i ){ } ...
- mysql5.5手册读书日记(4)
<?php /* InnoDB事务模型和锁定 15.2.10.1. InnoDB锁定模式 15.2.10.2. InnoDB和AUTOCOMMIT 15.2.10.3. InnoDB和TRANS ...
- centos安装后iptables基本设置
一.首先关闭防火墙#service iptables stop 二.查看状态,确认关闭#service iptables status 三.清除掉防火墙规则#iptables -F#iptables ...
- Flink -- Failover
JobManager failover LeaderLatch private synchronized void setLeadership(boolean newValue){ boole ...