map遍历的四种方式
原文 http://blog.csdn.net/dayanxuqun/article/details/26348277
以下是map遍历的四种方式:
- // 一、推荐只用value的时候用,都懂的。。。
- // Map.values()遍历所有的value,不遍历key
- for (String v : map.values()) {
- System.out.println("value= " + v);
- }
- // 二、取二次值,先取key再取value,建议只需要用key的时候使用,节省时间、空间
- // keySet遍历key和value
- for (String key : map.keySet()) {
- System.out.println("key= "+ key + " and value= " + map.get(key));
- }
- // 三、取一次值,一次把key和value全部取出
- // entrySet使用iterator遍历key和value
- Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String, String> entry = it.next();
- System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
- }
- // 四、推荐,尤其是容量大时,TreeMap尤其推荐
- // entrySet遍历key和value
- for (Map.Entry<String, String> entry : map.entrySet()) {
- System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
- }
另一个大牛曾经做过测试,感谢大牛!详见http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html
在此只把结论贴出,取之用之~:
3. 结论
3.1 如果你使用HashMap
同时遍历key和value时,keySet与entrySet方法的性能差异取决于key的具体情况,如复杂度(复杂对象)、离散度、冲突率等。换言之,取决于HashMap查找value的开销。entrySet一次性取出所有key和value的操作是有性能开销的,当这个损失小于HashMap查找value的开销时,entrySet的性能优势就会体现出来。例如上述对比测试中,当key是最简单的数值字符串时,keySet可能反而会更高效,耗时比entrySet少10%。总体来说还是推荐使用entrySet。因为当key很简单时,其性能或许会略低于keySet,但却是可控的;而随着key的复杂化,entrySet的优势将会明显体现出来。当然,我们可以根据实际情况进行选择
只遍历key时,keySet方法更为合适,因为entrySet将无用的value也给取出来了,浪费了性能和空间。在上述测试结果中,keySet比entrySet方法耗时少23%。
只遍历value时,使用vlaues方法是最佳选择,entrySet会略好于keySet方法。
在不同的遍历写法中,推荐使用如下写法,其效率略高一些:
- for (String key : map.keySet()) {
- value = map.get(key);
- }
- for (Entry<String, String> entry: map.entrySet()) {
- key = entry.getKey();
- value = entry.getValue();
- }
- for (String value : map.values()) {
- }
3.2 如果你使用TreeMap
同时遍历key和value时,与HashMap不同,entrySet的性能远远高于keySet。这是由TreeMap的查询效率决定的,也就是说,TreeMap查找value的开销较大,明显高于entrySet一次性取出所有key和value的开销。因此,遍历TreeMap时强烈推荐使用entrySet方法。
只遍历key时,keySet方法更为合适,因为entrySet将无用的value也给取出来了,浪费了性能和空间。在上述测试结果中,keySet比entrySet方法耗时少24%。
只遍历value时,使用vlaues方法是最佳选择,entrySet也明显优于keySet方法。
在不同的遍历写法中,推荐使用如下写法,其效率略高一些:
- for (String key : map.keySet()) {
- value = map.get(key);
- }
- for (Entry<String, String> entry: map.entrySet()) {
- key = entry.getKey();
- value = entry.getValue();
- }
- for (String value : map.values()) {
- }
map遍历的四种方式的更多相关文章
- java中Map遍历的四种方式
在java中所有的map都实现了Map接口,因此所有的Map(如HashMap, TreeMap, LinkedHashMap, Hashtable等)都可以用以下的方式去遍历. 方法一:在for循环 ...
- Java遍历Map对象的四种方式
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...
- 【转】Java遍历Map对象的四种方式
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...
- Map集合遍历的四种方式理解和简单使用-----不能for循环遍历
Map集合遍历的四种方式理解和简单使用 ~Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值 1:无非就是通过map.keySet()获取到值,然后 ...
- Java Enum枚举 遍历判断 四种方式(包括 Lambda 表达式过滤)
示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...
- Map集合遍历的四种方式理解和简单使用
~Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值 1:无非就是通过map.keySet()获取到值,然后根据键获取到值 for(String s:m ...
- Java中Map遍历的四种方案
在Java中如何遍历Map对象 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashM ...
- map遍历的四种方法
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- PHP数组循环遍历的四种方式
1.使用for循环遍历数组 conut($arr);用于统计数组元素的个数. for循环只能用于遍历,纯索引数组!!!! 如果存在关联数组,count统计时会统计两种数组的总 ...
随机推荐
- VMware+Windbg双机调试
虚拟机使用XP系统:
- python常用正则表达式
匹配特定数字:^[1-9]\d*//匹配正整数−[1−9]\d∗ //匹配负整数^-?[1-9]\d*//匹配整数[1−9]\d∗|0 //匹配非负整数(正整数 + 0)^-[1-9]\d*|0// ...
- Chapter 1 First Sight——25
"They are… very nice-looking." I struggled with the conspicuous understatement. 他们都很好看,我与轻 ...
- OpenCV使用边缘提取、腐蚀、轮廓进行车牌定位
http://blog.csdn.net/superdont/article/details/24935383 OpenCV使用边缘提取.腐蚀.轮廓进行车牌定位 2014-05-03 21:38 67 ...
- CCF考试真题题解
CCF考试认证:题解参考博客http://blog.csdn.net/u014578266/article/details/45221841 问题描述 试题编号: - 试题名称: 图像旋转 时间限制: ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- Stsadm 导入导出子站点
SharePoint通过stsadm备份和还原子网站(不是网站集) 大家都知道SharePoint的stsadm命令提供了很多便捷甚至是唯一的操作方法! 这里列出的所有命令:http://www. ...
- Java程序输出打字
代码: 效果:
- Java is Pass-by-Value!
Java is strictly pass-by-value. which means, when you pass a variable to a method, the method will j ...
- H5调用本地摄像头
<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"& ...