hashmap的遍历方法
How to iterate over the entries of a Map?
What is the order of iteration - if you are just using Map, then strictly speaking, there are no ordering guarantees. So you shouldn't really rely on the ordering given by any implementation. However, the SortedMap interface extends Map and provides exactly what you are looking for - implementations will aways give a consistent sort order.
NavigableMap is another useful extension - this is a SortedMap with additional methods for finding entries by their ordered position in the key set. So potentially this can remove the need for iterating in the first place - you might be able to find the specific entry you are after using the higherEntry, lowerEntry, ceilingEntry, or floorEntry methods. The descendingMap method even gives you an explicit method of reversing the traversal order.
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()) {
// ...
}
If you only need the values, use values():
for (Object value : map.values()) {
// ...
}
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();
// ...
}
One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator . However, changing item values is OK (see Map.Entry).
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
}
}
hashmap的遍历方法的更多相关文章
- HashMap集合-遍历方法
# HashMap集合-遍历方法 先定义好集合: public static void main(String[] args) { Map<String,String> onemap=ne ...
- HashMap有几种遍历方法?推荐使用哪种?
本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...
- java 遍历方法 及 数组,ArrayList,HashMap,HashSet的遍历
一,遍历方法的实现原理 1.传统的for循环遍历,基于计数器的: 遍历者自己在集合外部维护一个计数器,然后依次读取每一个位置的元素,当读取到最后一个元素后,停止.主要就是需要按元素的位置来读取元素. ...
- HashMap的四种遍历方法,及效率比较(简单明了)
https://yq.aliyun.com/ziliao/210955 public static void main(String[] args) { HashMap<Integer, Str ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- Map的五种遍历方法
package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...
- Map的遍历方法及String和其它类型的相互转化
Map的遍历方法: package com.lky.test; import java.util.HashMap; import java.util.Iterator; import java.uti ...
- HashMap的遍历和排序
1.HashMap的遍历 package com.sheepmu; import java.util.HashMap; import java.util.Iterator; import java.u ...
- List,Set,Map集合的遍历方法
List的三种实现:ArrayList(数组) LinkedList(链表) Vector(线程安全) List集合遍历方法: List<String> list = new Arra ...
随机推荐
- Indepence Mode 备份 weblogic
一般不在administation server 停止这个模式 管理服务器挂了,不会影响其他服务器的运行 备份一个domain copy 一个 /bin 把启动的脚本做一个修改 里面的doma ...
- Oracle 11.2.0.4在线(Online mode)打补丁14084247解决WRH$_ACTIVE_SESSION_HISTORY不会自动切割的问题
安装了Oracle Database Release 11.2.0.4之后,发现WRH$_ACTIVE_SESSION_HISTORY始终不会自动分割删除, 后来才发现需要应用补丁:14084 ...
- excel导入时候日期格式转成date
最近在做导入的时候发现,excel中设置数值格式是不能有日期的那些符号出现的,/ - : 之类的,否则就会变成数字到了java后台,设置成日期,比如 yyyy-mm-dd 到了后台也是数字,即距离19 ...
- 【技巧总结】Penetration Test Engineer[2]-Information gathering
2.信息收集 信息收集是属于前期交互阶段所需要了解的问题. 2.1.前期交互内容 签署授权文件:首要要和受测试方签订授权协议. 划定范围:指定了一个二级域名作为测试目标,那么其他二级域名在测试范围内. ...
- 项目中遇到的问题:Gradle传递性依赖冲突
问题描述: 在调用别人接口时,由于他们接口做了拦截处理在使用RestTemplate调用时必须要使用@Qualifier("他们封装好的类"),需要导入jar包 gradle方式导 ...
- vue2.0组件之间的传值
1.父子组件--props props:需要注意的是,虽然的是单向的数据流,但是如果传递的是数组或是对象这样的引用类型,子组件数据变化,父组件的数据通也会变化 子组件代码 <template&g ...
- 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)
Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...
- python基础--random模块
python使用random生成随机数 下面是主要函数random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0random.randint(a, b)生成的 ...
- HDU 3374 String Problem(KMP+最大(最小)表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...
- 20155225 2016-2017-2 《Java程序设计》第六周学习总结
20155225 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 java的输入输出系统 在重新指定标准输入输出时不同: 重新指定标准输入为文档输入时,是这样 ...