Python 官方文档:入门教程 => 点击学习
目录1、遍历方式1.1 迭代器 EntrySet1.2 迭代器 KeySet1.3 ForEach EntrySet1.4 ForEach KeySet1.5 Lambda 表达式1
@Test
public void test1() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "spring");
map.put(5, "springMVC");
map.put(6, "mybatis");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println(entry.geTKEy() + ":" + entry.getValue());
}
}
@Test
public void test2() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "Springmvc");
map.put(6, "MyBatis");
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key + ":" + map.get(key));
}
}
@Test
public void test3() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
@Test
public void test4() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
for (Integer key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
@Test
public void test5() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
}
@Test
public void test6() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.entrySet().stream().forEach((entry) -> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
}
@Test
public void test7() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.entrySet().parallelStream().forEach((entry) -> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
}
public class HashMapTraverseTest {
@Test
public void test1() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
@Test
public void test2() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key + ":" + map.get(key));
}
}
@Test
public void test3() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
@Test
public void test4() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
for (Integer key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
@Test
public void test5() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
}
@Test
public void test6() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.entrySet().stream().forEach((entry) -> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
}
@Test
public void test7() {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "JavaSE");
map.put(3, "JavaEE");
map.put(4, "Spring");
map.put(5, "SpringMVC");
map.put(6, "MyBatis");
map.entrySet().parallelStream().forEach((entry) -> {
System.out.println(entry.getKey() + ":" + entry.getValue());
});
}
}
使用 oracle 官方提供的性能测试工具 JMH(Java Microbenchmark Harness,JAVA 微基准测试套件)来测试一下这 7 种循环的性能。
使用 JMH 进行性能基准测试
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
</dependency>
<!-- Https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.23</version>
<scope>provided</scope>
</dependency>
直接复制粘贴即可!
@BenchmarkMode(Mode.AverageTime) // 测试完成时间
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 预热 2 轮,每次 1s
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 测试 5 轮,每次 1s
@Fork(1) // fork 1 个线程
@State(Scope.Thread) // 每个测试线程一个实例
public class HashMapCycleTest {
static Map<Integer, String> map = new HashMap() {{
// 添加数据
for (int i = 0; i < 100; i++) {
put(i, "val:" + i);
}
}};
public static void main(String[] args) throws RunnerException {
// 启动基准测试
Options opt = new OptionsBuilder()
// 要导入的测试类
.include(HashMapCycleTest.class.getSimpleName())
// 输出测试结果的文件
.output("D:/JAVA/面试/workplace/interview/jmh-hashMap.log")
.build();
// 执行测试
new Runner(opt).run();
}
@Benchmark
public void entrySet() {
// 遍历
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
Integer k = entry.getKey();
String v = entry.getValue();
}
}
@Benchmark
public void forEachEntrySet() {
// 遍历
for (Map.Entry<Integer, String> entry : map.entrySet()) {
Integer k = entry.getKey();
String v = entry.getValue();
}
}
@Benchmark
public void keySet() {
// 遍历
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Integer k = iterator.next();
String v = map.get(k);
}
}
@Benchmark
public void forEachKeySet() {
// 遍历
for (Integer key : map.keySet()) {
Integer k = key;
String v = map.get(k);
}
}
@Benchmark
public void lambda() {
// 遍历
map.forEach((key, value) -> {
Integer k = key;
String v = value;
});
}
@Benchmark
public void streamApi() {
// 单线程遍历
map.entrySet().stream().forEach((entry) -> {
Integer k = entry.getKey();
String v = entry.getValue();
});
}
public void parallelStreamApi() {
// 多线程遍历
map.entrySet().parallelStream().forEach((entry) -> {
Integer k = entry.getKey();
String v = entry.getValue();
});
}
}
运行程序,查看输出日志!
(1)第一次
(2)第二次
(3)第三次
上图解释:测试结论{测试的方法(Benchmark)、测试类型(Mode)、测试总次数(Cnt)、测试结果(Score)、误差(Error)、单位(Units)}
其中 Units 为 ns/op 意思是执行完成时间(单位为纳秒),而 Score 列为平均执行时间, ±
符号表示误差。
从以上结果可以看出,Lambda
和两个 EntrySet
的性能相近,接下来是 Stream API
单线程,然后是 KeySet
,性能最差。
从以上结果可以看出 entrySet
的性能比 keySet
的性能高出了一倍之多,因此我们应该尽量使用 entrySet
来实现 Map 集合的遍历,当然,如果熟练 Lambda
用 Lambda
更好咯,毕竟代码简洁。
如果想深入了解为啥性能会差别这么大,建议查看字节码文件进行分析。或者是使用 javap -c 类名.class
进行反编译,查看底层的实现。
到此这篇关于java中HashMap的7种遍历方式与性能分析的文章就介绍到这了,更多相关java HashMap遍历 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: java中HashMap的7种遍历方式与性能分析
本文链接: https://lsjlt.com/news/157249.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0