1.Iterate through the "entrySet" like so:

  1. public static void printMap(Map mp) {
  2. Iterator it = mp.entrySet().iterator();
  3. while (it.hasNext()) {
  4. Map.Entry pair = (Map.Entry)it.next();
  5. System.out.println(pair.getKey() + " = " + pair.getValue());
  6. it.remove(); // avoids a ConcurrentModificationException
  7. }
  8. }

2.If you're only interested in the keys, you can iterate through the "keySet()" of the map:

  1. Map<String, Object> map = ...;
  2.  
  3. for (String key : map.keySet()) {
  4. // ...
  5. }

3.If you only need the values, use "value()":

  1. for (Object value : map.values()) {
  2. // ...
  3. }

4.Finally, if you want both the key and value, use "entrySet()":

  1. for (Map.Entry<String, Object> entry : map.entrySet()) {
  2. String key = entry.getKey();
  3. Object value = entry.getValue();
  4. // ...
  5. }

  Summary,If you need only keys or values from the map, use method #2 or method #3. If you are stuck with older version of Java (less than 5) or planning to remove entries during iteration, you have to use method #1. Otherwise use method #4.

How to Iterate Over a Map in Java?(如何遍历Map)的更多相关文章

  1. Java中遍历Map集合的四种方法

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

  2. java中遍历map对象的多种方法

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

  3. Java中遍历map的四种方法 - 转载

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

  4. Java中遍历Map的几种方法

      转自: http://blog.csdn.net/wzb56/article/details/7864911 方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基 ...

  5. java中遍历MAP,嵌套map的几种方法

    java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>();    map.put("us ...

  6. JAVA中遍历Map和Set方法,取出map中所有的key

    Java遍历Set集合 1.迭代器遍历: Set<String> set = new HashSet<String>(); Iterator<String> it ...

  7. java 中遍历Map的几种方法

    方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基于map的key:map.keySet() 而每一类都有两种遍历方式: a.利用迭代器 iterator: b.利 ...

  8. java 如何遍历Map对象

    内容介绍 在java中遍历Map对象的方法. Map对象 Map<String,Object> map = new HashMap<>(); map.put("xia ...

  9. Java Collection - 遍历map的几种方式

    作者:zhaoguhong(赵孤鸿) 出处:http://www.cnblogs.com/zhaoguhong/ 本文版权归作者和博客园共有,转载请注明出处 ---------------- 总结 如 ...

随机推荐

  1. 转换成maven时报错

    转自:将项目加入maven管理时报错 将项目加入maven管理时报错: Convert to maven project: An internal error occurred during: “En ...

  2. Spring Security(02)——关于登录

    目录 1.1     form-login元素介绍 1.1.1    使用自定义登录页面 1.1.2    指定登录后的页面 1.1.3    指定登录失败后的页面 1.2     http-basi ...

  3. Exception fixing docBase for context

    原因在给tomcat的war包,传输过程中出了问题,或者不是2进制传输,重新用2进制传送到linux里的tomcat webapps 目录中  就解决了.

  4. HTML5根据浏览器获取经度和纬度(百度API)

    网页获取用户位置信息的办法1 调用百度地图的地图标注功能,通过百度地图API获取对应的经度和纬度进而获取地区信息 优点是比较准确,缺点是需要用户自己选择位置2 通过H5 geolocation属性获取 ...

  5. POJ 2054 Color a Tree#贪心(难,好题)

    题目链接 代码借鉴此博:http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html 其中关于max{c[fa]/t[fa]}贪心原则, ...

  6. 为什么推荐Zookeeper作注册中心

    Zookeeper的数据模型很简单,有一系列被称为ZNode的数据节点组成,与传统的磁盘文件系统不同的是,zk将全量数据存储在内存中,可谓是高性能,而且支持集群,可谓高可用,另外支持事件监听.这些特点 ...

  7. ajax通过设置Access-Control-Allow-Origin来实现跨域访问

    [在被请求的Response header中加入] // 指定允许其他域名访问(*代表所有域名)header('Access-Control-Allow-Origin:*');// 响应类型heade ...

  8. IntelliJ IDEA 13.1.4新建java web项目

    打开软件

  9. Openjudge-计算概论(A)-鸡兔同笼

    描述一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外).已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物输入第1行是测试数据的组数n,后面跟着n行输入.每组测试数 ...

  10. 2015 Multi-University Training Contest 9

    1001 Expression 式子不好推啊.见官方题解. 式子知道就方便了.处理好组合数和阶乘. 按区间长度从小到大递推完就好. # include <iostream> # inclu ...