方法一 通过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. [SDOI2010]捉迷藏

    嘟嘟嘟 k-d tree板儿题. 建完树后对每一个点求一遍最小和最大曼哈顿距离,是曼哈顿,不是欧几里得. #include<cstdio> #include<iostream> ...

  2. rman restore spfile from backup

    spfile一般在$ORACLE_HOME/dbs(linux)目录,如果你丢失了spfile并不会引起实例立刻停掉,你的实例可以继续操作,尽管当你restore spfile的时候需要关闭重启实例, ...

  3. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  4. ros卸载

    sudo apt-get purge ros-*sudo rm -rf /etc/rossudo rm -rf /opt/ros删除.bashrc中的source /opt/ros/indigo/se ...

  5. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  6. TLAB

    TLAB的全称是Thread Local Allocation Buffer,即线程本地分配缓存区,这是一个线程专用的内存分配区域. 由于对象一般会分配在堆上,而堆是全局共享的.因此在同一时间,可能会 ...

  7. JVM实践

    package com.lsw.classloader; import java.io.FileInputStream;import java.lang.reflect.Field;import ja ...

  8. WPF,ListView设置分组

    原文:WPF,ListView设置分组 今天遇到一个问题,就是在ListView中设置分组.想了很久在网上早了些资料作出一个例子. 分组字段也可以在后台中定义: CollectionView view ...

  9. C# 双击ListView出现编辑框可编辑,回车确认

    原文:C# 双击ListView出现编辑框可编辑,回车确认 //获取鼠标点击的项------API [DllImport("user32")] public static exte ...

  10. Ionic 中badge的应用

    app中如果有服务端推送过来的消息,用户没有查看的话,出现一个数字提醒,类似微信的那种效果. 在Ionic中的实现过程还是很简单的: <ion-tab title="首页" ...