map 常用方法
map遍历:
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
注意:for-each循环在java 5中被引入所以该方法只能应用于java 5或更高的版本中。
如果你遍历的是一个空的map对象,for-each循环将抛出NullPointerException,因此在遍历前你总是应该检查空引用。
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());
}
下面的方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。
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);
}
方法三使用Iterator遍历
用泛型:
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());
}
Map map =
new
HashMap();
Iterator entries = map.entrySet().iterator();
while
(entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println(
"Key = "
+ key +
", Value = "
+ value);
}
JDK1.5中,应用新特性For-Each循环
Map m = new HashMap();
for(Object o : map.keySet()){
map.get(o);
}
map 常用方法的更多相关文章
- java Map常用方法封装
java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...
- 浅谈集合框架三、Map常用方法及常用工具类
最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出.初学者对于本篇博客只建议作为参考,欢迎留言共同学习. 之前有介绍集合框架的体系 ...
- Java中Map常用方法总结以及遍历方式的汇总
一.整理: 看到array,就要想到角标. 看到link,就要想到first,last. 看到hash,就要想到hashCode,equals. 看到tree,就要想到两个接口.Comparable, ...
- std::map常用方法
map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair ...
- [js高手之路] es6系列教程 - Map详解以及常用api
ECMAScript 6中的Map类型是一种存储着许多键值对的有序列表.键值对支持所有的数据类型. 键 0 和 ‘0’会被当做两个不同的键,不会发生强制类型转换. 如何使用Map? let map = ...
- Map集合。
Map集合: java.util,Map<k,v> 特点:1.键值对 2.key-value一一对应 3.key不允许重复. Map常用实现类: java.util.HashMap< ...
- Java Map 集合实现类
Map 用于保存具有映射关系的数据,集合里会保存两组值,一组用于保存Map里的key,一组用于保存Map里的value,key与map可以是任何引用类型数据.Map的key不允许重复.key与valu ...
- Java集合----Map集合
Map Map 用于保存具有映射关系的数据,因此 Map 集合里保存着两组值,一组值用于保存 Map 里的 Key,另外一组用于保存 Map 里的 Value Map 中的 key 和 value 都 ...
- Java学习笔记——Map接口
Map接口 Map接口 Map接口中键和值一一映射. 可以通过键来获取值. 异常 NoSuchElementException:访问的值不存在 ClassCastException:对象类型错误 Un ...
随机推荐
- Asp.net工作流workflow实战(一)
最近开发一个项目用到了工作流引擎,之前研究过微软的workflow所有就用它了,距离上次用有一段时间了,好多东西有点模糊了,就在此处一遍写代码一遍回忆. 首先,在我的项目中新建了.netframwor ...
- Lua常用API
转自:http://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 1. 建一个新表 void lua_createtable (lua ...
- Python函数(十二)-迭代器
字符串,列表,元组,字典,集合,生成器这些能通过for循环来遍历的数据类型都是可迭代对象 可通过isinstance判断是不是可迭代对象 >>> from collections i ...
- DataTable列查询加排序
DataTable列查询加排序 DataRow[] drArray = dt.Select("ANLYCOM_ID='" + chSPrdtStblAnly.AnlyComId + ...
- RN控件之ProgressBarAndroid进度条
/** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import Rea ...
- 2、perl模块查询安装否
1.Perl 中每个包有一个单独的符号表,定义语法为:package mypack; 此语句定义一个名为 mypack 的包,在此后定义的所有变量和子程序的名字都存贮在该包关联的符号表中,直到遇到另一 ...
- 各版本Google浏览器下载地址
各版本谷歌浏览器下载地址 https://www.chromedownloads.net/chrome64win/
- Entity Framework Code-First(9.9):DataAnnotations - ForeignKey Attribute
DataAnnotations - ForeignKey Attribute: ForeignKey attribute can be applied to properties of a class ...
- 使用Paramiko的问题
在使用Paramiko远程登录的时候,会报sudo: sorry, you must have a tty to run sudo 切换到root用户,使用visudo命令,然后就会打开一个文本,在文 ...
- Other - 个人对知识讨论、分享等平台上抄袭乱象的看法
在某论坛上看到这样一句话,深表赞同.