HashMap遍历,推荐使用entrySet()
之前map遍历,偶尔会先去keyset然后再遍历keyset
比如
Map map = new HashMap();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
Object val = map.get(key);
}
但是记过sorarqube提示,这样效率比较低会产生两次循环,后台去网上查询发现确实还存在另一种遍历方式,通过entry set遍历。
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
以后记得用上面的遍历方式。
HashMap遍历,推荐使用entrySet()的更多相关文章
- HashMap中推荐使用entrySet方式遍历Map类集合KV而不是keySet方式遍历
我们先来做一个测试 public class HashMapTest { private HashMap<String, String> map = new HashMap<> ...
- HashMap遍历的两种方式,推荐使用entrySet()
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { ...
- HashMap 遍历的两种方式及性能比较
HashMap 是Java开发中经常使用的数据结构.相信HashMap 的基本用法你已经很熟悉了.那么我们该如何遍历HashMap 呢?哪种遍历方式的性能更好呢?本篇文章来为你解决这个疑惑. 一.Ha ...
- HashMap遍历时的性能对比
使用KeySet和EntrySet遍历的差别 public static void main(String[] args) { HashMap<Integer, Integer> hasM ...
- HashMap遍历的两种方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- [Java] HashMap遍历的两种方式
Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml第一种: Map map = new HashMap( ...
- Java HashMap 遍历方式探讨
JDK8之前,可以使用keySet或者entrySet来遍历HashMap,JDK8中引入了map.foreach来进行遍历. keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从 ...
- java 中 HashMap 遍历与删除
HashMap的遍历 方法一.这是最常见的并且在大多数情况下也是最可取的遍历方式 /** * 在键值都需要时使用 */ Map<Integer, Integer> map = new Ha ...
随机推荐
- jquerymobile知识点:实现toolbar下方显示,自定义图标!
css: .nav-glyphish-example .ui-btn .ui-btn-inner { padding-top: 40px !important; } .nav-glyphish-exa ...
- GridView导出Excel的超好样例
事实上网上有非常多关于Excel的样例,可是不是非常好,他们的代码没有非常全,读的起来还非常晦涩.经过这几天的摸索,最终能够完毕我想要导出报表Excel的效果了.以下是我的效果图. 一.前台的页面图 ...
- C# 动态创建出来的窗体间的通讯 delegate2
附件:http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate02.rar 窗体2 和窗体3 都是动态创建出来的 现在 FORM3.TE ...
- 标准库function类型的使用
14.44编写一个简单的桌面计算器使其能处理二元运算. #include<iostream> #include<map> #include<functional> ...
- java 中能否使用 动态加载的类(Class.forName) 来做类型转换?
今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...
- angularjs-yeoman环境配置
yum install npm -y npm install -g grunt-cli bower yo generator-karma-require generator-angular-requi ...
- Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC
Caused by: java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC 缺少com/sun/tools/inte ...
- sql存储过程通过ID删除两表中的数据。
CREATE OR REPLACE PROCEDURE del_p --建立名为del_p 的过程 IS CURSOR get_abid --简历名为get_abid的cursor 用来存放a表的id ...
- sql - 选出指定范围的行
Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...
- SQL生成一柱双色球
数据库环境:SQL SERVER 2005 以前用C/JAVA穷举双色球的所有排列,今天想着换成用SQL实现,只生成一柱双色球. 简单说下双色球的规则,双色球由红色球和蓝色球组成,每注投注号码由6个红 ...