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 higherEntrylowerEntryceilingEntry, 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的遍历方法的更多相关文章

  1. HashMap集合-遍历方法

    # HashMap集合-遍历方法 先定义好集合: public static void main(String[] args) { Map<String,String> onemap=ne ...

  2. HashMap有几种遍历方法?推荐使用哪种?

    本文已收录<面试精选>系列,Gitee 开源地址:https://gitee.com/mydb/interview HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...

  3. java 遍历方法 及 数组,ArrayList,HashMap,HashSet的遍历

    一,遍历方法的实现原理 1.传统的for循环遍历,基于计数器的: 遍历者自己在集合外部维护一个计数器,然后依次读取每一个位置的元素,当读取到最后一个元素后,停止.主要就是需要按元素的位置来读取元素. ...

  4. HashMap的四种遍历方法,及效率比较(简单明了)

    https://yq.aliyun.com/ziliao/210955 public static void main(String[] args) { HashMap<Integer, Str ...

  5. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  6. Map的五种遍历方法

    package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...

  7. Map的遍历方法及String和其它类型的相互转化

    Map的遍历方法: package com.lky.test; import java.util.HashMap; import java.util.Iterator; import java.uti ...

  8. HashMap的遍历和排序

    1.HashMap的遍历 package com.sheepmu; import java.util.HashMap; import java.util.Iterator; import java.u ...

  9. List,Set,Map集合的遍历方法

    List的三种实现:ArrayList(数组)  LinkedList(链表)  Vector(线程安全) List集合遍历方法: List<String> list = new Arra ...

随机推荐

  1. Strusts2笔记5--数据验证

    数据验证: 输入验证分为客户端验证与服务器端验证.客户端验证主要通过JavaScript脚本进行,而服务器端验证主要是通过Java代码进行验证. 分为以下四种情况: (1)手工编写代码,对Action ...

  2. jQuery Validate自定义金钱验证,是否为金额格式,保留两位小数,并支持千分制货币格式

    //自定义函数实现 isMoney: function (value, element){ // return this.optional(element) || /(^[1-9]([0-9]+)?( ...

  3. 10款常见MySQL高可用方案选型解读【转】

    我们在考虑MySQL数据库的高可用架构时,主要考虑如下几方面: 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据库的故障而中断. 用作备份. ...

  4. HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...

  5. jQuery-选择器-查找标签

    一.jQuery选择器 jQuery选择器就是帮助我们快速定位到一个或多个DOM节点 1.1  ID选择器 如果某个DOM节点有id属性,利用jQuery查找方式: <script src=&q ...

  6. python基础--shutil模块

    shutil模块提供了大量的文件的高级操作. 特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意 即便是更高级别的文件复制函数(shutil.cop ...

  7. Centos之目录处理命令

    linux中 关于目录 有几个重要概念 一个是 / 根目录  还有一个当前用户的家目录 比如 root用户的家目录是 /root  普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...

  8. 在MVC中如何使用vs2013编译调试razor cshtml

    打开mvc项目的csproj文件: <MvcBuildViews>false</MvcBuildViews> 改为 <MvcBuildViews>true</ ...

  9. more命令 less命令

    more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作.more名单中内置了若干快捷键,常用的有H(获得帮助信息),Enter(向下翻滚一行), ...

  10. 从字节码角度分析Byte类型变量b++和++b

    1. 下面是一到Java笔试题: public class Test2 { public void add(Byte b) { b = b++; } public void test() { Byte ...