How to Iterate Over a Map in Java?(如何遍历Map)
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)的更多相关文章
- Java中遍历Map集合的四种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- java中遍历map对象的多种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...
- Java中遍历map的四种方法 - 转载
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- Java中遍历Map的几种方法
转自: http://blog.csdn.net/wzb56/article/details/7864911 方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基 ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- JAVA中遍历Map和Set方法,取出map中所有的key
Java遍历Set集合 1.迭代器遍历: Set<String> set = new HashSet<String>(); Iterator<String> it ...
- java 中遍历Map的几种方法
方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基于map的key:map.keySet() 而每一类都有两种遍历方式: a.利用迭代器 iterator: b.利 ...
- java 如何遍历Map对象
内容介绍 在java中遍历Map对象的方法. Map对象 Map<String,Object> map = new HashMap<>(); map.put("xia ...
- Java Collection - 遍历map的几种方式
作者:zhaoguhong(赵孤鸿) 出处:http://www.cnblogs.com/zhaoguhong/ 本文版权归作者和博客园共有,转载请注明出处 ---------------- 总结 如 ...
随机推荐
- spring-security 配置文件
转自:spring-security学习笔记--配置文件 <?xml version="1.0" encoding="UTF-8"?> <be ...
- Spring Security(06)——AuthenticationProvider
目录 1.1 用户信息从数据库获取 1.1.1 使用jdbc-user-service获取 1.1.2 直接使用JdbcDaoImpl 1.2 PasswordEncode ...
- 使用jQuery的hover事件在IE中不停闪动的解决方法
在使用jQuery的hover事件时,经常会因为鼠标滑动过快导致菜单不停闪动的情况,相信很多朋友都遇到过自己做的纵向下拉菜单不停的收缩,非常的讨厌.今天在给一个网站设计菜单时也遇到了这个情况,结果在百 ...
- Java 反射 分析类和对象
Java 反射 分析类和对象 @author ixenos 摘要:优化程序启动策略.在运行时使用反射分析类的结构和对象 优化程序启动策略 在启动时,包含main方法的类被加载.它会加载所有它需要的类. ...
- Entity Framework删除数据
注:本文为个人学习摘录,原文地址:http://blog.csdn.net/itmaxin/article/details/47659817 最近在学EF,目前了解到删除操作有三种方式, 第一,官方推 ...
- openwrt杂记
/etc/config/wireless是在boot启动时生成的. 代码在/etc/init.d/boot中,如下: /sbin/wifi detect > /tmp/wireless.tmp ...
- MySQL(1) - 基础
参考资料: http://www.jianshu.com/p/91e3af27743f 一.MySQL介绍以及安装 1.1 MySQL介绍 MariaDB数据库管理系统是MySQL的一个分支,主要由开 ...
- UNIX基础--用户和基本账户管理
账户类型 系统账户 系统账户运行服务. 系统用户是那些要使用诸如DNS. 邮件, web等服务的用户. 使用帐户的原因就是安全: 如果所有的用户都由超级用户来运行, 那它们就可以不受约束地做任何事情. ...
- LYNC2013介绍和基础架构准备角色
LYNC2013部署系列PART1:LYNC2013介绍和基础架构准备 前言:LYNC 2013发布已经很久了,本人一直在进行相关的学习和测试,在有限的资源条件下,把能够模拟出来的角色进行了安装部署, ...
- ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)
1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...