方法一 通过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.keySet遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet使用iterator遍历key和value:
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.entrySet遍历key和value
key= 1 and value= value1
key= 2 and value= value2
key= 3 and value= value3
通过Map.values()遍历所有的value,但不能遍历key
value= value1
value= value2
value= value3

【Java集合的详细研究4】Java中如何遍历Map对象的4种方法的更多相关文章

  1. (转载)Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  2. 转!! Java中如何遍历Map对象的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  3. 【转】Java中如何遍历Map对象的4种方法

    原文网址:http://blog.csdn.net/tjcyjd/article/details/11111401 在Java中如何遍历Map对象 How to Iterate Over a Map ...

  4. Java中如何遍历Map对象的4种方法

    在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...

  5. Java中遍历Map对象的4种方法

    java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等). HashMap<Inte ...

  6. Java遍历Map对象的四种方法

    在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...

  7. Java 遍历Map对象的4种方法

    http://blog.csdn.net/tjcyjd/article/details/11111401

  8. (转)在Java中如何遍历Map对象

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  9. Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...

随机推荐

  1. Spark本地运行成功,集群运行空指针异。

    一个很久之前写的Spark作业,当时运行在local模式下.最近又开始处理这方面数据了,就打包提交集群,结果频频空指针.最开始以为是程序中有null调用了,经过排除发现是继承App导致集群运行时候无法 ...

  2. 转载 【.NET基础】--委托、事件、线程(1) https://www.cnblogs.com/chengzish/p/4559268.html

    [.NET基础]--委托.事件.线程(1)   1,委托 是存放方法的指针的清单,也就是装方法的容器 A, 新建winform项目[01委托],项目中添加dg_SayHi.cs 委托类 用于存储方法 ...

  3. Oracle数据文件改名

    1. open状态改名   SYS@userdata>select tablespace_name,file#,file_name,v.status,v.enabled from dba_dat ...

  4. Shell获取指定时间

    时区基本知识 相差一个时区(经度15度)时间相差一小时.简单计算口诀:1.同一个时区的相差时间用减法,2.不同时区的相差时间用加法.3东加西减.例如一,东八区是8点,问东1区几点.根据上面口诀1,算: ...

  5. leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列

    字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...

  6. 如何在Qt Creator中导入图标资源

    本文主要描述如何在Qt Creator中创建资源文件,并的打入导入图标文件. 查看图标资源文件时,可以在项目的工程文件上鼠标单击右键-Open With-资源编辑器,效果如下图所示: 在项目的工程文件 ...

  7. 理解Vuex的辅助函数mapState, mapActions, mapMutations用法

    在讲解这些属性之前,假如我们项目的目录的结构如下: ### 目录结构如下: demo1 # 工程名 | |--- dist # 打包后生成的目录文件 | |--- node_modules # 所有的 ...

  8. Ubuntu触摸屏校准命令

    1.sudo apt-get install utouch 2.sudo apt-get install xinput-calibrator 3.sudo xinput_calibrator(校准两次 ...

  9. 2-微信小程序开发(开发界面说明,按钮点击切换显示内容)

    说一个功能,大家在用微信实现控制设备的时候,是不是都在为绑定设备发愁. 我看了很多厂家的微信控制,大部分都只是可以用微信给设备配网,但是没有做用微信绑定的. 一般做绑定都是用设备的MAC地址. 这里我 ...

  10. BZOJ 5475: [WC 2019] 数树

    WC2019 数树 解决了一个心头大患 考试的时候本人太智障了QwQ 本文的参考链接,膜了一发rqy的题解 题目链接 Subtask 0 好像可以直接做... 推一推就能发现,是$y^k$,其中$k$ ...