In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use the cycle of HashMap". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's first take a look at each traversal:
Using entries to realize traversal of Map in for Loop
Public static void forEachEntries () {for (Map.Entry entry: map.entrySet ()) {String mapKey = entry.getKey (); String mapValue = entry.getValue ();}}
Traversing key in a for loop
Public static void forEachKey () {for (String key: map.keySet ()) {String mapKey = key; String mapValue = map.get (mapKey);}}
Traversing value in a for loop
Public static void forEachValues () {for (String key: map.values ()) {String val = key;}}
Iterator traversal
Public static void forEachIterator () {Iterator entries = map.entrySet (). Iterator (); while (entries.hasNext ()) {Entry entry = entries.next (); String key = entry.getKey (); String value = entry.getValue ();}}
ForEach jdk1.8 traversal
Public static void forEach () {map.forEach ((key, val)-> {String key1 = key; String value = val;});}
Stream jdk1.8 traversal
Map.entrySet () .stream () .forEach ((entry)-> {String key = entry.getKey (); String value = entry.getValue ();})
Streamparallel jdk1.8 traversal
Public static void forEachStreamparallel () {map.entrySet () .parallelStream () .forEach ((entry)-> {String key = entry.getKey (); String value = entry.getValue ();});}
These are some of the common traversal methods for map. Let's write a test case to see which of these traversal methods are the most efficient. The following test cases are based on JMH. First, pom is introduced.
Org.openjdk.jmh jmh-core 1.23 org.openjdk.jmh jmh-generator-annprocess 1.23 provided
Some of the factors that may affect the results of jmh tests will not be described in detail here, but can be written in great detail by referring to the first link at the end of the article. And why the test case is written in this way (all in order to eliminate the impact of JIT on the test code) this is the link on the official website: write the test code as follows:
Package com.workit.autoconfigure.autoconfigure.controller; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; import org.openjdk.jmh.results.format.ResultFormatType; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry Import java.util.UUID; import java.util.concurrent.TimeUnit / * * @ author: official account: java Finance * @ Date: * @ Description: Wechat search [java Finance] reply 666 * / @ State (Scope.Thread) @ Warmup (iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) @ Measurement (iterations = 5, time = 1) TimeUnit = TimeUnit.SECONDS) @ Fork (1) @ BenchmarkMode (Mode.AverageTime) @ OutputTimeUnit (TimeUnit.NANOSECONDS) public class InstructionsBenchmark {public static void main (String [] args) throws RunnerException {Options opt = new OptionsBuilder () .include (InstructionsBenchmark.class.getSimpleName ()) .result ("result.json") .resultFormat (ResultFormatType.JSON) .build () New Runner (opt). Run ();} static final int BASE = 42; static int add (int key,int val) {return BASE + key + val;} @ Param ({"1", "10", "100", "1000", "10000", "100000"}) int size; private static Map map / / initialization method to perform @ Setup (Level.Trial) public void init () {map = new HashMap (size); for (int I = 0; I) before all Benchmark runs
< size; i++) { map.put(i, i); } } /** * 在for循环中使用entries实现Map的遍历: */ @Benchmark public static void forEachEntries(Blackhole blackhole) { for (Map.Entry entry : map.entrySet()) { Integer mapKey = entry.getKey(); Integer mapValue = entry.getValue(); blackhole.consume(add(mapKey,mapValue)); } } /** * 在for循环中遍历key */ @Benchmark public static StringBuffer forEachKey(Blackhole blackhole) { StringBuffer stringBuffer = new StringBuffer(); for (Integer key : map.keySet()) { // Integer mapValue = map.get(key); blackhole.consume(add(key,key)); } return stringBuffer; } /** * 在for循环中遍历value */ @Benchmark public static void forEachValues(Blackhole blackhole) { for (Integer key : map.values()) { blackhole.consume(add(key,key)); } } /** * Iterator遍历; */ @Benchmark public static void forEachIterator(Blackhole blackhole) { Iterator entries = map.entrySet().iterator(); while (entries.hasNext()) { Entry entry = entries.next(); Integer key = entry.getKey(); Integer value = entry.getValue(); blackhole.consume(add(key,value)); } } /** * forEach jdk1.8遍历 */ @Benchmark public static void forEachLamada(Blackhole blackhole) { map.forEach((key, value) ->{blackhole.consume (add (key,value));};} / * * forEach jdk1.8 traversal * / @ Benchmark public static void forEachStream (Blackhole blackhole) {map.entrySet () .stream () .forEach ((entry)-> {Integer key = entry.getKey (); Integer value = entry.getValue () Blackhole.consume (add (key,value));}) @ Benchmark public static void forEachStreamparallel (Blackhole blackhole) {map.entrySet () .parallelStream () .forEach ((entry)-> {Integer key = entry.getKey (); Integer value = entry.getValue (); blackhole.consume (add (key,value));});}}
The running results are as follows: "Note: the running environment idea 2019.3 jdk1.8 is 64-bit for windows 7. "
Benchmark (size) Mode Cnt Score Error Units InstructionsBenchmark.forEachEntries 1 avgt 5 10.021 ±0.224 ns/op InstructionsBenchmark.forEachEntries 10 avgt 5 71.709 ±2.537 ns/op InstructionsBenchmark.forEachEntries 100 avgt 5 738.873 ±12.132 ns/op InstructionsBenchmark.forEachEntries 1000 avgt 5 7804.431 ±136.635 ns/op InstructionsBenchmark.forEachEntries 10000 avgt 5 88540.345 ±14915.682 ns/op InstructionsBenchmark.forEachEntries 100000 avgt 5 1083347.001 ±136865.960 ns/op InstructionsBenchmark.forEachIterator 1 avgt 5 10.675 ±2.532 ns/op InstructionsBenchmark.forEachIterator 10 avgt 5 73.934 ±4.517 ns/op InstructionsBenchmark .forEachIterator 100 avgt 5 775.847 ±198.806 ns/op InstructionsBenchmark.forEachIterator 1000 avgt 5 8905.041 ±1294.618 ns/op InstructionsBenchmark.forEachIterator 10000 avgt 5 98686.478 ±10944.570 ns/op InstructionsBenchmark.forEachIterator 100000 avgt 5 1045309.216 ±36957.608 ns/op InstructionsBenchmark.forEachKey 1 avgt 5 18.478 ±1.344 ns/op InstructionsBenchmark.forEachKey 10 avgt 5 76.398 ±12.179 ns/op InstructionsBenchmark.forEachKey 100 avgt 5 768.507 ±23.892 ns/op InstructionsBenchmark.forEachKey 1000 avgt 5 11117.896 ±1665.021 ns/op InstructionsBenchmark.forEachKey 10000 avgt 5 84871.880 ±12056.592 ns/op InstructionsBenchmark.forEachKey 100000 avgt 5 1114948.566 ±65582.709 ns/op InstructionsBenchmark.forEachLamada 1 avgt 5 9.444 ±0.607 ns/op InstructionsBenchmark.forEachLamada 10 avgt 5 76.125 ±5.640 ns/op InstructionsBenchmark.forEachLamada 100 avgt 5 861.601 ±98.045 ns/op InstructionsBenchmark.forEachLamada 1000 avgt 5 7769.714 ±1663.914 ns/op InstructionsBenchmark.forEachLamada 10000 avgt 5 73250.238 ±6032.161 ns/op InstructionsBenchmark.forEachLamada 100000 avgt 5 836781.987 ±72125.745 ns/op InstructionsBenchmark.forEachStream 1 avgt 5 29.113 ±3.275 ns/op InstructionsBenchmark.forEachStream 10 avgt 5 117.951 ±13.755 ns/op InstructionsBenchmark.forEachStream 100 avgt 5 1064.767 ±66.869 ns/op InstructionsBenchmark.forEachStream 1000 avgt 5 9969.549 ±342.483 ns/op InstructionsBenchmark.forEachStream 10000 avgt 5 93154.061 ±7638.122 ns/op InstructionsBenchmark.forEachStream 100000 avgt 5 1113961.590 ±218662.668 ns/op InstructionsBenchmark.forEachStreamparallel 1 avgt 5 65.466 ±5.519 ns/op InstructionsBenchmark.forEachStreamparallel 10 avgt 5 2298.999 ± 721.455 ns/op InstructionsBenchmark.forEachStreamparallel 100 avgt 5 8270.759 ±1801.082 ns/op InstructionsBenchmark.forEachStreamparallel 1000 avgt 5 16049.564 ±1972.856 ns/op InstructionsBenchmark.forEachStreamparallel 10000 avgt 5 69230.849 ±12169.260 ns/op InstructionsBenchmark.forEachStreamparallel 100000 avgt 5 638129.559 ±14885.962 ns/op InstructionsBenchmark.forEachValues 1 avgt 5 9.743 ±2.770 ns/op InstructionsBenchmark.forEachValues 10 avgt 5 70.761 ±16.574 ns/op InstructionsBenchmark.forEachValues 100 avgt 5 745.069 ±329.548 ns/op InstructionsBenchmark.forEachValues 1000 avgt 5 7772.584 ±1702.295 ns/op InstructionsBenchmark.forEachValues 10000 avgt 5 74063.468 ±23752.678 ns/op InstructionsBenchmark.forEachValues 100000 avgt 5 994057.370 ±279310.867 ns/op
We can find that when the amount of data is small, the cycle efficiency of forEachEntries, forEachIterator and lamada is the same, but the efficiency of forEachStreamarallel is lower, only when the amount of data reaches more than 10000 parallelStream is reflected. So there is no need to worry too much about which way to choose which cycle mode to use, in fact, the efficiency between each way is still very little. Just choose what suits you.
That's all for "how to use the HashMap Loop". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.