方法一 通过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. c#中开发ActiveX的学习笔记

    1.为什么要用ActiveX? 网页本身的功能是有限的,要想实现一些网页本身不支持的功能,比如:网页上的p2p视频播放,就得靠ActiveX这种古老的技术. 2.c#能开发ActiveX吗? 严格意义 ...

  2. [转]免费数学神器!再复杂的公式,只要有照片就能转成LaTeX

    转自:https://mp.weixin.qq.com/s/ja7UGr742DtYglGU6TT4qg 又一数学神器来袭! 这是一个帮你快速把数学公式图片转成LaTeX代码的工具,名为Snip,可以 ...

  3. PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  4. C++ 指针常量和常量指针

    1.指针常量(*const):对应指针变量,即指针本身是常量,指针指向的内容可以被修改. 2.常量指针(const*):常量的指针,即指针指向的内容不能被修改,但指针本身是变量,可以被修改.

  5. 大功率DC-DC方案

    三端稳压芯片只适合于小功率器件的直流稳压(电流<1A):如7805,这个电路是可以应付大多数情况的: 如果出现大功率的器件就需要采用新的稳压方案,可以参考下图方案: https://www.bi ...

  6. day83

    今日内容 rest_framework序列化 首先序列化需要对写一个类继承serializers.Serializer 方式一:在models的publish写一个__str__方法返回出版社名字 p ...

  7. curl发送json格式数据

    php的curl方法详细的见官方手册. curl_setopt用法:  http://www.php.net/manual/en/function.curl-setopt.php <?php $ ...

  8. CF1060E Sergey and Subways 假的点分治

    题目传送门:http://codeforces.com/problemset/problem/1060/D 题意:给出$N$个点的一棵树,现在将距离为$2$的点之间连一条边,求所有点对之间最短路的和, ...

  9. .NetCore实践篇:分布式监控系统zipkin踩坑之路(二)

    前言 <牧神记>有一句话说的好,破心中神.当不再对分布式,微服务,CLR畏惧迷茫的时候,你就破了心中神. zipkin复习 第一篇: .Net架构篇:思考如何设计一款实用的分布式监控系统? ...

  10. VS2017一步一步断点调试解决Dapper语句出现的Bug

    最近再做一个项目,出现一个小bug,bug虽小,但是却要命啊.下面我show下我解决问题的方法. View层代码: @model List<mhq.Blog.Model.Blog> < ...