1.Iterate through the "entrySet" like so:

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

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

Map<String, Object> map = ...;

for (String key : map.keySet()) {
// ...
}

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

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

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

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

  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. Ambari安装组件出错

    Caught an exception while executing custom service command: <class 'ambari_agent.AgentException.A ...

  2. delphi中nil、null、UnAssigned区别

    nil:空指针,空地址,对象也是指针,所以可以object := nil;null:null是一个未定义值的变量,既不是0也不代表空字符串,它是未定义的.http://www.delphibasics ...

  3. C# 语言规范_版本5.0 (第2章 词法结构)

    1. 词法结构 1.1 程序 C# 程序 (program) 由一个或多个源文件 (source file) 组成,源文件的正式名称是编译单元 (compilation unit)(第 9.1 节). ...

  4. CreateCompatibleBitmap 需要注意的问题

    不要使用CreateCompatibleDC得到的内存DC作为其参数,应使用真实DC,否则图片不能显示

  5. java代码块 静态、非静态

    Java虚拟机的内存分区:Java栈.堆.方法区.本地方法栈.PC寄存器.还有一个常量池的概念,虚拟机会为每种类型分配一个常量池,而不是实例. 例如有一个类有很多子类,那么在父类定义的final变量, ...

  6. toggle的使用心得

    点击同一个标签可以实现不同效果 或者几个效果 以前一般都是if 判断的 逻辑还有判断比较繁琐 看啦手册后 发现这个功能可以不用判断的自动循环点击事件 比以前的简单好用 主要用法:$("td& ...

  7. json处理三部曲之第二曲:利用Jackson处理json

    利用Jackson处理json需要导入的jar包(2以上版本的): <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.c ...

  8. Windows命令查看文件MD5码

    D:\>certutil -hashfile md5test.txt MD5 MD5 哈希(文件 md5test.txt): d6 f6 bb 38 b5 6b 67 8f 34 9b e4 d ...

  9. win安装Theano

    艰辛的安装Theano过程,把其中遇到的问题记录下来,三台机子都尝试了安装Theao,系统分别为:A机:win7 64-bit(笔记本).B机:win7 64-bit(台式机).C机:win8 64- ...

  10. [Python] spides

    摘要:本文将介绍用Python进行的爬虫的各种练习以及涉及到的各种知识,包括Http协议,cookie等等 工具 Fiddler Python默认不用代理,所以fiddler不能截取它的包.如果想用用 ...