网站首页 > 博客文章 正文
最近在面试的时候笔试碰到一道关于map的题,请手写出map遍历效率最高的方法。
关于map遍历的方式相信大家都知道,但是各个方法的一个效率高低可能有些人平常没有注意,所以在这做了一个简单的测试。
public class MapBianLiXiaoLvBiJiao {
private static Map<Integer,Integer> map=new HashMap<>();
static {
for (int i=0;i<10000;i++){
int j=0;
j+=i;
map.put(i,j);
}
}
public static void main(String[] args) {
MapBianLiXiaoLvBiJiao mapBianLiXiaoLvBiJiao = new MapBianLiXiaoLvBiJiao();
mapBianLiXiaoLvBiJiao.foreachMethod();
mapBianLiXiaoLvBiJiao.keySetMethod();
mapBianLiXiaoLvBiJiao.iteratorMethod();
mapBianLiXiaoLvBiJiao.streamForeachMethod();
}
// 通过foreach遍历entry
public void foreachMethod(){
Long startTime=System.currentTimeMillis();
for (Map.Entry<Integer,Integer> entry:map.entrySet()){
Integer key= entry.getKey();
Integer value=entry.getValue();
}
long endTime=System.currentTimeMillis();
System.out.println("foreach花费时间为:"+(endTime-startTime));
}
// 通过遍历keySet并获取value
public void keySetMethod(){
Long startTime=System.currentTimeMillis();
for (Integer key:map.keySet()){
Integer value=map.get(key);
}
long endTime=System.currentTimeMillis();
System.out.println("keySet遍历花费时间为:"+(endTime-startTime));
}
// 通过迭代器iterator遍历
public void iteratorMethod(){
Long startTime=System.currentTimeMillis();
Iterator<Map.Entry<Integer,Integer>> it=map.entrySet().iterator();
while (it.hasNext()){
Map.Entry<Integer,Integer> entry=it.next();
Integer key=entry.getKey();
Integer value=entry.getValue();
}
long endTime=System.currentTimeMillis();
System.out.println("iterator遍历花费时间为:"+(endTime-startTime));
}
// 通过map.forEach
public void streamForeachMethod(){
Long startTime=System.currentTimeMillis();
map.forEach((key,value) -> {
Integer key1=key;
Integer value1=value;
});
long endTime=System.currentTimeMillis();
System.out.println("转换为流遍历花费时间为:"+(endTime-startTime));
}
}
执行结果如下:
foreach花费时间为:7
keySet遍历花费时间为:5
iterator遍历花费时间为:1
转换为流遍历花费时间为:122
经过上面的小测试可以看出,通过iterator迭代器对map进行遍历的方式效率是最高的,而map.forEach()遍历的效率是最低
猜你喜欢
- 2024-12-26 Java 8 Stream 处理大数据集:实战与优化
- 2024-12-26 面试官:Java8 lambda 表达式 forEach 如何提前终止?
- 2024-12-26 Javascript中,forEach和map到底有什么区别?
- 2024-12-26 Excel VBA之For Each遍历循环的应用
- 2024-12-26 为什么建议使用 for…of 循环而不是 foreach 循环呢
- 2024-12-26 前端开发map和foreach区别,map遍历方式用法介绍
- 2024-12-26 Rust语言从入门到精通系列 - 零基础掌握Stream流迭代器
- 2024-12-26 java集合类之java中集合类有哪些?如何分类?
- 2024-12-26 【一分钟学Java】之List
- 2024-12-26 Java并行流:一次搞定多线程编程难题,让你的程序飞起来!
你 发表评论:
欢迎- 最近发表
-
- Python 中 必须掌握的 20 个核心函数—len()函数
- 用PLC的指针实现字符串转byte(Codesys平台)一文极简搞懂指针
- EXCEL如何用函数读取复杂字符串中的数据
- 2025-07-19:计算字符串的镜像分数。用go语言,给定一个字符串 s
- 2025-07-10:字符相同的最短子字符串Ⅰ。用go语言,给定一个长度
- 基于物理特征融合与机器学习的多井协同钻井速率实时预测与优化(
- [电子学报文章精选]一种基于特征融合的恶意代码快速检测方法
- 强大的可视化流程图编辑神器 — LogicFlow
- 前端框架太卷了!字节企业级框架Arco Design Mobile开源了
- Vue独立组件——11个最佳Vue.js日期选择器组件
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- flutterrun (59)
- powershellfor (73)
- messagesource (71)
- plsql64位 (73)
- vueproxytable (64)
- npminstallsave (63)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- nacos启动失败 (64)
- ssh-add (70)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- qcombobox样式表 (68)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)