如何遍历Map对象
方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时
这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
方法二、通过Map.keySet遍历key,通过键找值value遍历(效率低),普遍使用,二次取值
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key = " + key + ", Value = " + value);
方法三 如果只需要map中的键或者值,你可以通过Map.keySet或Map.values来实现遍历,而不是用entrySet。在for-each循环中遍历keys或values。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//遍历map中的键
for (Integer key : map.keySet()) { System.out.println("Key = " + key); }
//遍历map中的值
for (Integer value : map.values()) { System.out.println("Value = " + value); }
方法四:通过Map.entrySet使用iterator遍历key和value
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
public static void main(String[] args) {
init();
traversal();
}
// HashMap按照哈希算法来存取键对象,有很好的存取性能
private static HashMap<String, String> hMap = new HashMap<>();
// TreeMap实现了SortedMap接口,能对键对象进行排序。支持自然排序和客户化排序两种方式。
private static TreeMap<String, String> tMap = new TreeMap<>();
// map的赋值
public static void init() {
hMap.put("1", "value1");
hMap.put("2", "value2");
hMap.put("3", "value3");
}
// map的遍历
public static void traversal() {
// 第一种:普遍使用,二次取值
System.out.println("通过Map.keySet遍历key和value:");
for (String key : hMap.keySet()) {
System.out.println("key= " + key + " and value= " + hMap.get(key));
}
// 第二种
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
Iterator<Map.Entry<String, String>> it = hMap.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 : hMap.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
// 第四种
System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
for (String v : hMap.values()) {
System.out.println("value= " + v);
}
}
如何遍历Map对象的更多相关文章
- 转!! Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- 【转】Java中如何遍历Map对象的4种方法
原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...
- java中遍历map对象的多种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...
- (转)在Java中如何遍历Map对象
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- (转载)Java中如何遍历Map对象的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- java 如何遍历Map对象
内容介绍 在java中遍历Map对象的方法. Map对象 Map<String,Object> map = new HashMap<>(); map.put("xia ...
- Java中如何遍历Map对象的4种方法
在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...
- Java遍历Map对象的四种方法
在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...
- Java中遍历Map对象的方法
方法一: 在for-each循环中使用entries来遍历 这是最常见的遍历方式,在需要获取key和value时使用. Map<Integer, Integer> map = new Ha ...
- 【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使 ...
随机推荐
- escape()、encodeURI()、encodeURIComponent() 编码解码
escape().encodeURI().encodeURIComponent()区别详解 JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encode ...
- Win/Lin 双系统时间错误的调整 (转)
Win/Lin 双系统时间错误的调整 http://jingyan.baidu.com/article/154b46317b25ca28ca8f41e8.html | 浏览:1070 | 更新:201 ...
- Freemarker入门
Freemarker入门 工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId& ...
- 禁用firefox 56自动更新
firefox 56支持旧式扩展,这很重要! 它却自动更新,简单地关了也不行,很是牛氓! ========== -备份C:\Users\用户名\AppData\Roaming\Mozilla\Fire ...
- 关于dumper和mysqldump的
最近在做版本更新 使用dumper在连接阿里云的RDS的时候 老是提示 需要超级管理员权限 无能只能使用系统自带的mysqldump了 语句简单方便 而且导出的表 没有加载库信息 mysqldump ...
- 19.纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
原文地址:https://segmentfault.com/a/1190000014836748 感想: 把原元素隐藏,利用伪元素::before 和 ::after 各取上下一半 clip-path ...
- python中的clear
1 a = { 2 "name":"dlrb", 3 "age":25, 4 "height":168 5 } 6 a. ...
- twisted的DelayedCall
>>> l=[,,,,] >>> del l[:]#只是删除列表的部分元素,列表仍然存在 >>> l [, ] #reactor循环执行的函数de ...
- vs2008 "不安全代码只会在使用 /unsafe 编译的情况下出现"的解决方法
原因是:在编译的代码里面有不安全类型unsafe方法或类! 解决方法:将项目的“可编译不安全代码”属性设置为true就可以了,方法如下:项目属性对话框->配置属性->生成->允许不安 ...
- Kali 局域网 DNS 劫持
<一> 所需工具 1: Kali-linux-2017 2: ettercap 0.8.2 3: web 服务器, 这里以 node 为例 <二> 原理 1: DNS劫持 ...