Detailed explanation of the selection of map traversal mode in Java

1. Description

For the traversal of map in Java, many articles recommend using entryset, which is much more efficient than keyset. The reason is: the entryset method gets the set of all keys and values at one time; The keyset only gets the set of keys. For each key, you need to find an additional value in the map, which reduces the overall efficiency. So what is the actual situation?

In order to understand the real gap in ergodic performance, including the differences in different scenarios such as ergodic key + value, ergodic key and ergodic value, I tried to conduct some comparative tests.

2. Comparative test

At the beginning, only a simple test was conducted, but the results showed that the performance of keyset was better. This makes me very confused. Don't you say that entryset is obviously better than keyset? In order to further verify, different test data are used for more detailed comparative test.

2.1 test data

2.1. 1 HashMap test data

Hashmap-2, the size is 1 million, the key and value are string, and the key values are 50, 100, 150, 200,..., 50000000:

2.1. 2 treemap test data treemap-1, the size is 1 million, the key and value are both string, and the key values are 1, 2, 3... 1000000:

Treemap-2 is 1 million in size. Both key and value are string. The values of key are 50, 100, 150, 200,..., 50000000, which is more discrete:

2.2 test scenario

Test three scenarios by using various writing methods of keyset, entryset and values: traversing key + value, traversing key and traversing value.

2.2. 1 Key + value

Keyset traversal key + value (writing method 1):

Keyset traversal key + value (writing method 2):

Entryset traversal key + value (writing method 1):

Entryset traversal key + value (writing method 2):

2.2. 2 traversal key

Keyset traversal key (writing method 1):

Keyset traversal key (writing method 2):

Entryset traversal key (writing method 1):

Entryset traversal key (writing method 2):

2.2. 3 traversal value

Keyset traversal value (write 1):

Keyset traversal value (write 2):

Entryset traversal value (writing method 1):

Entryset traversal value (writing method 2):

Values traverses value (write 1):

Values traverses value (write 2):

2.3 test results

2.3. 1 HashMap test results

2.3. 2 treemap test results

3. Conclusion

3.1 if you use HashMap

When traversing key and value at the same time, the performance difference between keyset and entryset methods depends on the specific situation of key, Such as complexity (complex object), dispersion, collision rate, etc. in other words, it depends on the cost of HashMap searching for value. The operation of entryset fetching all keys and values at one time has performance cost. When this loss is less than the cost of HashMap searching for value, the performance advantage of entryset will be reflected. For example, in the above comparison test, when the key is the simplest numeric string, keyset It may be more efficient and take 10% less time than entryset. In general, entryset is recommended. Because when the key is very simple, its performance may be slightly lower than that of the keyset, but it is controllable; With the complexity of key, the advantages of entryset will be obvious. Of course, we can choose according to the actual situation. When only traversing the key, the keyset method is more appropriate, because the entryset takes out the useless value, wasting performance and space. In the above test results, keyset takes 23% less time than entryset.

When traversing only value, the vlaues method is the best choice, and the entryset method is slightly better than the keyset method.

In different traversal writing methods, the following writing method is recommended, which is slightly more efficient:

3.2 if you use treemap

When traversing key and value at the same time, unlike HashMap, the performance of entryset is much higher than that of keyset. This is determined by the query efficiency of treemap, that is, the cost of finding value in treemap is large, which is significantly higher than the cost of fetching all keys and values at one time in entryset. Therefore, the entryset method is strongly recommended when traversing a treemap.

When only traversing the key, the keyset method is more appropriate, because the entryset takes out the useless value, wasting performance and space. In the above test results, keyset takes 24% less time than entryset.

When traversing only value, the vlaues method is the best choice, and the entryset method is obviously better than the keyset method.

In different traversal writing methods, the following writing method is recommended, which is slightly more efficient:

summary

The above is all about the detailed explanation of the selection of map traversal mode in Java. I hope it will be helpful to you. Interested friends can refer to: analysis of map internal storage mode in Java, code examples of java bean and map mutual conversion methods, and Discussion on mutual conversion between object and map, etc. If you have any questions, please leave a message. Xiaobian will reply to you in time. Thank you for your support for the programming tips website.

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
分享
二维码
< <上一篇
下一篇>>