遍历map的四方方法
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "value1");
map.put("2", "value2");
map.put("3", "value3");
//第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : map.keySet()) {
System.out.println("key= "+ key + " and value= " + map.get(key));
}
//第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第三种:推荐,尤其是容量大时
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
//第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : map.values()) {
System.out.println("value= " + v);
}
}
遍历map的四方方法的更多相关文章
- Java中遍历Map对象的方法
方法一: 在for-each循环中使用entries来遍历 这是最常见的遍历方式,在需要获取key和value时使用. Map<Integer, Integer> map = new Ha ...
- java中遍历Map几种方法
java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点. 先初始化一个map: publ ...
- Java 遍历Map集合的方法
方法一:通过Map.keySet,遍历key和value Map<String, Object> map = new HashMap<>(); for (String key ...
- 用来遍历map集合的方法
map集合是以键值对进行存储值的,所以遍历map集合无非就是获取键和值,根据实际需求,进行获取键和值. 1.无非就是通过map.keySet()获取到值,然后根据键获取到值. for(String s ...
- Java中四种遍历Map对象的方法
方法一:在for-each循环中使用entry来遍历,通过Map.entrySet遍历key和value,这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Int ...
- JAVA中遍历Map和Set方法,取出map中所有的key
Java遍历Set集合 1.迭代器遍历: Set<String> set = new HashSet<String>(); Iterator<String> it ...
- 遍历Map集合的方法
创建一个MAP的栗子: Map<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put(& ...
- 遍历Map集合四中方法
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- [转载] Java 遍历 Map 的 5 种方式
目录 1 通过 keySet() 或 values() 方法遍历 2 通过 keySet 的 get(key) 获取值 3 通过 entrySet 遍历 4 通过迭代器 Iterator 遍历 5 通 ...
随机推荐
- ZOJ - 2615 Cells
注意数组别开太小了,代码照着训练经典打的: #include <iostream> #include <sstream> #include <cstdio> #in ...
- Visual Assist X
老师推荐的一款vc++6.0插件 visual assistx_百度百科 http://baike.baidu.com/view/4439152.htm?fr=aladdin 下载 VC++6.0助手 ...
- 【Quick 3.3】资源脚本加密及热更新(二)资源加密
[Quick 3.3]资源脚本加密及热更新(二)资源加密 注:本文基于Quick-cocos2dx-3.3版本编写 一.介绍 在前一篇文章中介绍了代码加密,加密方式是XXTEA.对于资源文件来说,同样 ...
- ☀【Grunt】no such file or directory, imagemin
JPG image optimization not workinghttps://github.com/gruntjs/grunt-contrib-imagemin/issues/16 npm un ...
- 使用 Azure Site Recovery 灾难恢复至 Azure 的功能现已正式发布
ABHISHEK A. HEMRAJANI 云 + Enterprise项目经理 自我们宣布发布使用 Azure SiteRecovery 灾难恢复至 Azure的功能预览版以来,这几个月着实令人 ...
- nginx 健康检查和负载均衡机制分析
nginx 是优秀的反向代理服务器,这里主要讲它的健康检查和负载均衡机制,以及这种机制带来的问题.所谓健康检查,就是当后端出现问题(具体什么叫出现问题,依赖 于具体实现,各个实现定义不一样),不再往这 ...
- 新浪微博2.5.1 for Android 去广告
新浪微博更新到2.5.1版,就开始来广告了,伤不起啊... 亲,看到没,手机屏幕就那么一小块,还要往里面塞东西,另外是一个在后台运行的AdCenter服务. 所需要用到的工具有:apktool,JD- ...
- HDU 2122
思路:prime,考虑重边!!! #include<stdio.h> #include<string.h> int map[1001][1001],dist[1001]; in ...
- 从修复 testerhome(rubychina)网站的一个 bug 学习 ruby&rails on ruby
前言 testerhome: http://testerhome.com/topics/1480 对于一个差点脱离前沿技术人,想要学习ruby,就意味着要放弃熟悉的操作系统windows,熟悉的ide ...
- visualbox使用(二)
1.安装VirtualBox的[增强功能]2.VirtualBox的[设备]->[共享文件夹],添加固定分配,如D:\Java, 名称Java3.执行如下命令#cd /mnt#mkdir w_j ...