1、遍历方式
1.1 迭代器 EntrySet
@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());
}
}
1.2 迭代器 KeySet
@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));
}
}
1.3 ForEach EntrySet
@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());
}
}
1.4 ForEach KeySet
@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));
}
}
1.5 Lambda 表达式
@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);
});
}
1.6 Stream API 单线程
@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());
});
}
1.7 Stream API 多线程
@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());
});
}
1.8 代码汇总
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());
});
}
}
2、性能分析
使用 Oracle 官方提供的性能测试工具 JMH(Java Microbenchmark Harness,JAVA 微基准测试套件)来测试一下这 7 种循环的性能。
使用 JMH 进行性能基准测试
2.1 引入依赖
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.23</version>
<scope>provided</scope>
</dependency>
2.2 编写测试类
直接复制粘贴即可!
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@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();
});
}
}
2.3 测试结果
运行程序,查看输出日志!
(1)第一次
(2)第二次
(3)第三次
2.4 分析
上图解释:测试结论{测试的方法(Benchmark)、测试类型(Mode)、测试总次数(Cnt)、测试结果(Score)、误差(Error)、单位(Units)}
其中 Units 为 ns/op 意思是执行完成时间(单位为纳秒),而 Score 列为平均执行时间, ± 符号表示误差。
从以上结果可以看出,Lambda 和两个 EntrySet 的性能相近,接下来是 Stream API 单线程,然后是 KeySet ,性能最差。
2.5 总结
从以上结果可以看出 entrySet 的性能比 keySet 的性能高出了一倍之多,因此我们应该尽量使用 entrySet 来实现 Map 集合的遍历,当然,如果熟练 Lambda 用 Lambda 更好咯,毕竟代码简洁。
如果想深入了解为啥性能会差别这么大,建议查看字节码文件进行分析。或者是使用 javap -c 类名.class 进行反编译,查看底层的实现。
|