Summary of various traversal and deletion methods of HashMap
•
Java
preface
Several ways of HashMap traversal
1、 Iterator traversal
Iterative entryset
@Test
public void testEntrySet() {
Iterator<Map.Entry<String,String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String,String> entry = iterator.next();
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
}
Iterative keyset
@Test
public void testKeySet() {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + " --> " + map.get(key));
}
}
2、 Foreach traversal
Traverse entryset
@Test
public void testForEachEntrySet() {
for (Map.Entry<String,String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
}
Traverse keyset
@Test
public void testForEachKeySet() {
for (String key : map.keySet()) {
System.out.println(key + " --> " + map.get(key));
}
}
3、 Lambda traversal
@Test
public void testLambda() {
map.forEach((key,value) -> {
System.out.println(key + " --> " + value);
});
}
4、 Streamapi traversal
@Test
public void testStream() {
map.entrySet().stream().forEach(entry -> {
System.out.println(entry.getKey() + " --> " + entry.getValue());
});
}
@Test
public void testParallelStream() {
map.entrySet().parallelStream().forEach(entry -> {
System.out.println(entry.getKey() + " --> " + entry.getValue());
});
}
Performance comparison of various traversal methods
Introducing jmh performance testing framework
<dependencies>
<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.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
Write test
@BenchmarkMode(Mode.Throughput) //测试类型 吞吐量
@Warmup(iterations = 2,time = 1,timeUnit = TimeUnit.SECONDS) //预热2轮,每次1秒
@Measurement(iterations = 5,time = 3,timeUnit = TimeUnit.SECONDS) //测试5轮,每次3s
@Fork(1) //fork 1个线程
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread) //每个测试线程1个实例
public class HashMapBenchmark {
static Map<String,String> map = new HashMap<String,String>() {
{
put("author","天乔巴夏");
put("title","HashMap的各种遍历方式");
put("url","www.hyhwky.com");
}
};
public static void main(String[] args) throws RunnerException {
System.out.println(System.getProperties());
Options opt = new OptionsBuilder()
.include(HashMapBenchmark.class.getSimpleName())
.output(System.getProperty("user.dir") + "\\HashMapBenchmark.log")
.build();
new Runner(opt).run();
}
@Benchmark
public void testEntrySet() {
Iterator<Map.Entry<String,String> entry = iterator.next();
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
}
@Benchmark
public void testKeySet() {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + " --> " + map.get(key));
}
}
@Benchmark
public void testForEachEntrySet() {
for (Map.Entry<String,String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
}
}
@Benchmark
public void testForEachKeySet() {
for (String key : map.keySet()) {
System.out.println(key + " --> " + map.get(key));
}
}
@Benchmark
public void testLambda() {
map.forEach((key,value) -> {
System.out.println(key + " --> " + value);
});
}
@Benchmark
public void testStream(){
map.entrySet().stream().forEach(entry ->{
System.out.println(entry.getKey() + " --> " + entry.getValue());
});
}
@Benchmark
public void testParallelStream(){
map.entrySet().parallelStream().forEach(entry ->{
System.out.println(entry.getKey() + " --> " + entry.getValue());
});
}
}
The test results are as follows
Benchmark Mode Cnt score Error Units
HashMapBenchmark.testEntrySet thrpt 5 6.929 ± 1.042 ops/ms
HashMapBenchmark.testForEachEntrySet thrpt 5 7.025 ± 0.627 ops/ms
HashMapBenchmark.testForEachKeySet thrpt 5 7.024 ± 0.481 ops/ms
HashMapBenchmark.testKeySet thrpt 5 6.769 ± 1.231 ops/ms
HashMapBenchmark.testLambda thrpt 5 7.056 ± 0.300 ops/ms
HashMapBenchmark.testParallelStream thrpt 5 5.784 ± 0.216 ops/ms
HashMapBenchmark.testStream thrpt 5 6.826 ± 0.578 ops/ms
Several ways of HashMap iterative deletion
Iterator deletion
@Test
public void testRemoveIterator() {
Iterator<Map.Entry<String,String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().getKey();
if ("title".equals(key)) {
iterator.remove();
}
}
System.out.println(map);
}
Removeif of lambda
@Test
public void testRemoveLambda() {
map.keySet().removeIf("title"::equals);
System.out.println(map);
}
Filter of streamapi
@Test
public void testRemoveStream() {
map.entrySet()
.stream()
.filter(m -> !"title".equals(m.getKey()))
.forEach(entry -> {
System.out.println(entry.getKey() + " --> " + entry.getValue());
});
}
Reference articles
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码