“This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

preface

Hello, everyone. Map sets are often used in projects to encapsulate data, and they have been used recently. How efficient are they when traversing data in a Map?

Map

Map provides a more general method of storing elements. The Map interface stores a set of key-value objects and provides a mapping from key to value. Each element in the Map collection contains a key object and a value object.

Initialize data

First of all, in order to facilitate the introduction of Map traversal and their execution efficiency, this test initializes a part of the data. This test will traverse the Map set in four ways and test their efficiency. The four methods are map.keyset (), map.entryset (), Iterator, and map.values(). This section describes how to perform performance comparison. The test data constructed is as follows:


Map<String.String> map = new HashMap<String.String> (); map.put("1"."Nuggets");
map.put("2"."More code");
map.put("3"."The nuggets don't stop.");

Copy the code

Map.keySet()

The first way to iterate over elements in a Map is to iterate over keys and values using map. keySet.

for (String key : map.keySet()) {
    System.out.println("key= "+ key + " and value= " + map.get(key));
}
Copy the code

Map.keyset () traverses the elements of the Map collection and produces the following output:

The Iterator Iterator

The second method uses iterators. Iterator iterators are used to iterate over keys and values. The test code is as follows:


Iterator<Map.Entry<String.String>> it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<String.String> entry = it.next();
    System.out.println("Through map.entryset.key =" + entry.getKey() + " and value= " + entry.getValue());
}

Copy the code

Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator

Map.entrySet()

The third method uses map. entrySet to iterate over keys and values. The test code is as follows:


for (Map.Entry<String.String> entry : map.entrySet()) {
    System.out.println("Traversal through map. entrySet. Key =" + entry.getKey() + " and value= " + entry.getValue());
}

Copy the code

Map.entryset () iterates through the elements of the Map collection and produces the following output:

Map.values()

The fourth way traverses Map elements through map.values (), traversing all values but not keys. The test code is as follows:

for (String v : map.values()) {
    System.out.println("value= " + v);
}
Copy the code

Map.values() traverses the values of the Map set elements. The following output is displayed:

You can traverse the Map in the preceding four ways. In the fourth way, you can only obtain the Map value. In the first three ways, you can obtain the key and value. Now that you’re familiar with the centralized traversal, let’s test the performance.

The performance test

The four Map traversal modes are described above, and the following performance tests are performed for the four traversal modes. See which traversal performance is better, high efficiency. 100 data execution time is as follows:

1000 data execution time is as follows:

10000 data execution time is as follows:

100000 data execution time is as follows:

The execution time of 1,000,000 pieces of data is as follows:10000000 pieces of data execution time is as follows:The statistical table is as follows:

Implement way Article 100. Article 1000. Article 10000. Article 100000. Article 1000000. Article 10000000.
Map.keySet() 2ms 10ms 54ms 414ms 5215ms 52741ms
Iterator 2ms 7ms 46ms 417ms 4735ms 48822ms
Map.entrySet() 2ms 11ms 51ms 439ms 4708ms 48186ms
Map.values() 0ms 9ms 41ms 385ms 4696ms 46896ms

The table shows the amount of data in the table and the elapsed time of Map traversal in different ways. When the amount of data is small, the efficiency of using Iterator is relatively high. When the amount of data reaches a certain level, the efficiency of using map.entryset () can be significantly improved. Of course, these data are only generated when the code author tests the time, may be based on the machine and other external conditions, there will be some errors, just for your reference to learn.

conclusion

Ok, the above is the summary of the Map set traversal mode and its efficiency, thank you for reading, I hope you like it, if it is helpful to you, welcome to like the collection. If there are shortcomings, welcome comments and corrections. See you next time.

About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.