【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式
参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html
==================================
java紫色代表迭代方式
效率测试:100W
==================================
HashMap迭代方式1:entrySet迭代
public static void main(String[] args) {
Map<String,String> hashMap = new HashMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("hashMap存储开始时间-->"+beginTime);
for (int i = 0; i < ; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("hashMap存储结束时间-->"+endTime);
System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("hashMap【entrySet方式】读取开始时间-->"+endTime);
//可以使用外部定义变量
for (Map.Entry<String,String> entry : hashMap.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
long endTime2 = System.currentTimeMillis();
System.out.println("hashMap【entrySet方式】读取结束时间-->"+endTime2);
System.out.println("hashMap【entrySet方式】读取消耗:"+(endTime2-endTime)+"ms");
}


===================================================================
HashMap迭代方式2:keySet迭代
public static void main(String[] args) {
Map<String,String> hashMap = new HashMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("hashMap存储开始时间-->"+beginTime);
for (int i = 0; i < ; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("hashMap存储结束时间-->"+endTime);
System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("hashMap【keySet方式】读取开始时间-->"+endTime);
//可以使用外部定义变量
Set<String> keySet = hashMap.keySet();
for (String s : keySet) {
System.out.println(s+":"+hashMap.get(s));
}
long endTime2 = System.currentTimeMillis();
System.out.println("hashMap【keySet方式】读取结束时间-->"+endTime2);
System.out.println("hashMap【keySet方式】读取消耗:"+(endTime2-endTime)+"ms");
}


=================================================================
HashMap迭代方式3:forEach方式
public static void main(String[] args) {
Map<String,String> hashMap = new HashMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("hashMap存储开始时间-->"+beginTime);
for (int i = 0; i < ; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("hashMap存储结束时间-->"+endTime);
System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("hashMap【forEach方式】读取开始时间-->"+endTime);
//不能使用外部定义变量 除非final类型 例如:List
hashMap.forEach((k,v)->{
System.out.println(k +":"+v);
});
long endTime2 = System.currentTimeMillis();
System.out.println("hashMap【forEach方式】读取结束时间-->"+endTime2);
System.out.println("hashMap【forEach方式】读取消耗:"+(endTime2-endTime)+"ms");
}


=======================================================================================================
TreeMap迭代:entrySet方式
public static void main(String[] args) {
Map<String,String> hashMap = new TreeMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("TreeMap存储开始时间-->"+beginTime);
for (int i = 0; i < 1000000; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("TreeMap存储结束时间-->"+endTime);
System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("TreeMap【entrySet方式】读取开始时间-->"+endTime);
//可以使用外部定义变量
for (Map.Entry<String,String> entry : hashMap.entrySet()){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
long endTime2 = System.currentTimeMillis();
System.out.println("TreeMap【entrySet方式】读取结束时间-->"+endTime2);
System.out.println("TreeMap【entrySet方式】读取消耗:"+(endTime2-endTime)+"ms");
}


=================================================
TreeMap迭代:keySet方式
public static void main(String[] args) {
Map<String,String> hashMap = new TreeMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("TreeMap存储开始时间-->"+beginTime);
for (int i = 0; i < 1000000; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("TreeMap存储结束时间-->"+endTime);
System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("TreeMap【keySet方式】读取开始时间-->"+endTime);
//可以使用外部定义变量
Set<String> keySet = hashMap.keySet();
for (String s : keySet) {
System.out.println(s+":"+hashMap.get(s));
}
long endTime2 = System.currentTimeMillis();
System.out.println("TreeMap【keySet方式】读取结束时间-->"+endTime2);
System.out.println("TreeMap【keySet方式】读取消耗:"+(endTime2-endTime)+"ms");
}


==========================================================
TreeMap迭代:forEach方式
public static void main(String[] args) {
Map<String,String> hashMap = new TreeMap<>();
long beginTime = System.currentTimeMillis();
System.out.println("TreeMap存储开始时间-->"+beginTime);
for (int i = 0; i < 1000000; i++) {
hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString());
}
long endTime = System.currentTimeMillis();
System.out.println("TreeMap存储结束时间-->"+endTime);
System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms");
System.out.println("TreeMap【forEach方式】读取开始时间-->"+endTime);
//不能使用外部定义变量 除非final类型 例如:List
hashMap.forEach((k,v)->{
System.out.println(k +":"+v);
});
long endTime2 = System.currentTimeMillis();
System.out.println("TreeMap【forEach方式】读取结束时间-->"+endTime2);
System.out.println("TreeMap【forEach方式】读取消耗:"+(endTime2-endTime)+"ms");
}


=========================================================
100W级别
基本判断【并不准确】:HashMap存储效率高,读取效率也比较高
TreeMap存储效率低,读取效率差不多
=========================================================
1000W级别
【java】TreeMap/HashMap的循环迭代中 keySet和entrySet和forEach方式 + map的几种迭代方式的更多相关文章
- 细说java中Map的两种迭代方式
曾经对java中迭代方式总是迷迷糊糊的,今天总算弄懂了.特意的总结了一下.基本是算是理解透彻了. 1.再说Map之前先说下Iterator: Iterator主要用于遍历(即迭代訪问)Collecti ...
- 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法
具体见第三阶段scala-day01中的文档(scala编程基础---基础语法) 1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...
- JavaScript高级编程——Array数组迭代(every()、filter()、foreach()、map()、some(),归并(reduce() 和reduceRight() ))
JavaScript高级编程——Array数组迭代(every().filter().foreach().map().some(),归并(reduce() 和reduceRight() )) < ...
- Map中keySet和entrySet的区别
在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...
- java map在JSTL EL中的小应用--<c:forEach>遍历Map<>泛型
准 备 数 据 :(自己准备吧少年,考验你时候到了!!) /** 结构示意图: 类型: List集合 map对象 LIst集合 Person类对象 String name : int age mLis ...
- Java中Map的4种遍历方式
第一种方式:这是平常用的最多也最可取的一种遍历方式. for (Map.Entry<String, Object> entry : map.entrySet()) { System.out ...
- 空数组在以下三种遍历中均不可更改:forEach、map和for...in
首先,我们要知道对于forEach.map和for...in三种遍历,在不是空数组的情况下,要想实现更改原数组的方法,代码如下: var list = [1,2,3,4]; var list1 = [ ...
- Java 程序测试_循环语句中的break和continue
package test; public class Loop_Statement { public static void main(String [] args) { String[] newba ...
- 实在没想到系列——HashMap实现底层细节之keySet,values,entrySet的一个底层实现细节
我在看HashMap源码的时候发现了一个没思考过的问题,在这之前可以说是完全没有思考过这个问题,在一开始对这个点有疑问的时候也没有想到居然有这么个语法细节存在,弄得我百思不得其解,直到自己动手做实验改 ...
随机推荐
- IOS笔记047-代理传值和block传值
在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据 ...
- C++ STL map容器的说明测试1
// maptest.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" /*********************************** ...
- Python学习-day10(番外篇) 阻塞IO 非阻塞IO 同步IO 异步IO
这个章节的内容是关于IO的概念,谈一谈什么是 阻塞IO 非阻塞IO 同步IO 异步IO.以下摘要是我对这四种IO的一个形象理解. 场景是去去银行办理业务.节点有三个,1)到银行提交申请:2)取号:3) ...
- caffe工程配置问题
一开始是碰到没有caffe/caffe.hpp文件的问题,不知道怎么弄.通过百度,知道了在makefile文件里加入头文件路径和库文件路径就行. 首先是caffe.pb.h丢失问题,解决方法:http ...
- 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现
SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...
- 【转】log4js在PM2的cluster模式下大坑
请直接查看原文:https://blog.yourtion.com/fix-log4js-with-pm2-not-work.html 之前一直使用 debug 还有 console.log 去打日志 ...
- ftp下出现“当前的安全设置不允许从该位置下载文件”提示
在资源管理器中使用ftp协议下载文件时,提示“当前的安全设置不允许从该位置下载文件”,下载失败. 解决方法: 1.在自己的电脑上打开Internet选项
- POJ3207 Ikki's Story IV - Panda's Trick 【2-sat】
题目 liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikk ...
- Python数据结构之列表
1.Python列表是Python内置的数据结构对象之一,相当于数组 2.列表用[] 包含,内有任意的数据对象,每一个数据对象以 ,逗号分隔,每隔数据对象称之为元素 3.Python列表是一个有序的序 ...
- HDU2767 Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...