ava:Map借口及其子类HashMap三
ava:Map借口及其子类HashMap三
HashMap常用子类(异步非安全线程,性能高; Hashtable:同步的安全线程,性能低)
map(HashMap)中的key,value可以通过 Set<E>,或者Conllection<E>来接收.
Map<String,Integer> allMap = new HashMap<String, Integer>();
allMap.put("zhangsan", 1);
allMap.put("zhangsan", 2);
allMap.put("lisi", 3);
allMap.put("tianqi", 4); //Integer value = allMap.get("zhangsan");
//System.out.println(value); Collection<Integer> keys = allMap.values();
Iterator<Integer> iter = keys.iterator();
while(iter.hasNext())
{
Integer str = iter.next();
System.out.println( str + "、" );
}
结果:
3、
2、
4、
注意事项:
Map不能直接使用Iterator类输出
在集合的标准操作中所有的集合内容最好使用Iterator进行输出,但在Map接口中并没有明确的定义出这样的操作。如果没有的话,则必须深入了解Map的机制。
在Map中虽然是以一对值得形式出现的,可是真正的保存的还是一个单独的对象,即:程序key->alue的存放在一个对象之中,之后将对象加入到集合里。
Map.Entry,Map实体,从定义格式上可以发现,此接口属于STATIC静态声明的接口。而且是一个内部接口。
对于Map和Map.Entry的关系,如下图:
MAP.Entry [ 一组对象数据]
Map.Entry [ 一组对象数据]
Map.Entry [一组对象数据]
....... <-------------------- 增加元素 Map.Entry[ key=>value 一组对象数据]
所以:下面就可以给出Map接口使用Iterator输出的标准操作:
1.通过Map接口中的:Set<Map.Entry<K, V>> entrySet()方法取得Set集合
2.通过Set接口,为Iterator进行初始化操作
3.通过Iterator取得每一个Map.Entry
4.通过Map.Entry将KEY与VALUE分离。
例子:
Map<String, Integer> allSet = new HashMap<String, Integer>();
allSet.put("zhangsan", 1);
allSet.put("zhangsan", 2);
allSet.put("lisi", 3);
allSet.put("wangwu", 4); Set<Map.Entry<String,Integer>> allList = allSet.entrySet();
Iterator<Map.Entry<String,Integer>> iter = allList.iterator();
while(iter.hasNext())
{
Map.Entry<String, Integer> map = iter.next();
System.out.println( map.getKey() + "-->" + map.getValue() ); }
Set<Map.Entry<String,Integer>> allList = allSet.entrySet();
Iterator<Map.Entry<String,Integer>> iter = allList.iterator();
while(iter.hasNext())
{
Map.Entry<String, Integer> map = iter.next();
System.out.println( map.getKey() + "-->" + map.getValue() ); }
结果:
lisi-->3
zhangsan-->2
wangwu-->4
或者Foreach循环:
Map<String, Integer> allSet = new HashMap<String, Integer>();
allSet.put("zhangsan", 1);
allSet.put("zhangsan", 2);
allSet.put("lisi", 3);
allSet.put("wangwu", 4); //或者
for(Map.Entry<String, Integer> map: allSet.entrySet())
{
System.out.println( map.getKey() + "-->" + map.getValue());
}
ava:Map借口及其子类HashMap三的更多相关文章
- java:Map借口及其子类HashMap五,identityHashMap子类
java:Map借口及其子类HashMap五,identityHashMap子类 了解:identityHashMap子类 一般情况下,标准的Map,是不会有重复的key值得value的,相同的key ...
- java:Map借口及其子类HashMap四
java:Map借口及其子类HashMap四 使用非系统对象作为key,使用匿名对象获取数据 在Map中可以使用匿名对象找到一个key对应的value. person: public class Ha ...
- java:Map借口及其子类HashMap二
java:Map借口及其子类HashMap二 重点:所有的集合必须依赖Iterator输出 Map<String, Integer> map = new HashMap<String ...
- java:Map借口及其子类
java:Map借口及其子类 Conllection是保存单值最大得父接口(即没有key的数据),那么Map是保存的内容是一对键值的数据,即KEY->VALUE的形式保存,如电话簿等. Map常 ...
- HashMap三百问
文章目录: 一.JDK1.7之HashMap 二.JDK1.8之HashMap 三.Hashtable JDK1.7之HashMap 1. 定义 HashMap实现了Map接口,继承AbstractM ...
- Map接口及其子类
Map接口操作的是一对对象,即二元偶对象,Map接口中的每一个元素都使用"key--value"的形式存储在集合中. SortedMap接口是排序接口,仅仅要是实现了此接口的子类, ...
- Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】
Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...
- Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法
Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的区别与用法
Java容器类List.ArrayList.Vector及map.HashTable.HashMap的区别与用法 ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数 ...
随机推荐
- openWRT自学---对官方的开发指导文档的解读和理解 记录2:如何控制内核模块的编译
openwrt对于kernel module的处理分两类:随内核主线而来的kernel module 和 其他作为独立project的kernel module.而这两种,openwrt将采用相同的模 ...
- ThinkPHP3.1URL分发BUG修正
请先留意以下PHP脚本 PHP脚本A(http://127.0.0.1:8110/test.php): $url = 'http://127.0.0.1:8110/demo.php'; //curl请 ...
- BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs
题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...
- commons-dbutils:1.6 ——java.sql.SQLException: 不支持的特性
描述:使用jdbc创建连接后,使用commons-dbutils-1.6 数据库工具类,查询报错如下:java.sql.SQLException: 不支持的特性 Query: 经过测试跟踪在commo ...
- byte[] 、Bitmap与Drawbale 三者直接的转换
经常遇到这种类似头疼的问题 byte[] .Bitmap与Drawbale 三者直接的转换 1.byte[] ->Bitmap Bitmap Bitmap = BitmapFactory.dec ...
- 自定义 ViewController 容器转场
本文转载至 http://blog.csdn.net/yongyinmg/article/details/40621463 在话题 #5 中,Chris Eidhof 向我们介绍了 iOS7 引入的新 ...
- BestCoder Round #63 (div.2)
感觉有些无聊的比赛. A 暴力枚举下就行 B 简单的dp,但是wa了一发后就去先把C做了,然后发现如果输入的100个数,是如1,2,3,4,...,100,然后k=50,个数为c(100,50).果断 ...
- Linux下安装 activemq 并指定jdk 1.8
1. 下载安装包 <apache-activemq-5.15.4-bin.tar.gz> 下载地址:https://pan.baidu.com/s/18xzjBAchjWqsHNA1HuY ...
- SDOI2012 Round1 day2 拯救小云公主(dis)解题报告
#include<cstdio> #include<cmath> #include<iostream> using namespace std; typedef l ...
- android菜鸟学习笔记23----ContentProvider(三)利用内置ContentProvider监听短信及查看联系人
要使用一个ContentProvider,必须要知道的是它所能匹配的Uri及其数据存储的表的结构. 首先想办法找到访问短信及联系人数据的ContentProvider能接受的Uri: 到github上 ...