Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a Map collection in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the Map collection in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is Map

Different from the single-column linear structure of List, Map in Java provides a storage set of two-column mapping, which can provide one-to-one data processing capability. the first column in the two columns is called key, and the second column is value. A key can only appear once in a Map, and the only corresponding value in the Map can be obtained through a key. In practical applications, a key can be used to quickly locate the corresponding value.

There are three characteristics of Map:

Contains key-value pairs

Key unique

The value corresponding to the key is unique

Second, the common traversal methods of Map collections

There are three common ways to traverse the Map collection:

The way you use keySet

The way you use entrySet

How to use values () to take a value

In all three ways, there are corresponding values for for loop traversal and Iterator traversal.

KeySet is a collection of keys, and the type in Set is the type of key.

EntrySet is a collection of key-value pairs, and the type in Set is that Map.Entry,Entry is a key-value pair.

KeySet (): key can only be fetched through get () after iteration

EntrySet (): after iteration, you can e.getKey (), e.getValue () takes key and value, and returns the Entry interface.

Third, traversal performance comparison

For each is equivalent to the display call Iterator, except for the third way (for each map.keySet ()), and then calls the get get method, the other three methods have the same performance. In this case, the hash hash is better, and if the hash algorithm is poor, the third method will be more time-consuming.

1. When the number of data elements is relatively large, the traversal efficiency of entrySet () is faster than that of keySet () for two reasons:

(1) one reason is that keySet is equivalent to traversing the Set collection of key twice, and the second time is to get the value value through key and map.get (key) during each traversal.

(2) the second reason is that when map.get (key), the bottom layer is to get a hash value according to the hashCode value of key through the hash algorithm, and then map it as an index to the index position of the corresponding table array. This is an intensive calculation and consumes a lot of CPU. If there are a large number of elements, it will cause the CPU utilization to soar and affect the response speed, while the elements returned by entrySet () are all Map.Enpty types. Key and value are one of the attributes of this class, and entry.getKey () and entry.getValue () must be very efficient.

2. However, when the number of data elements is small, the traversal efficiency of keySet () is faster than that of entrySet ().

3. Values () is the Collection that returns the collection of all value of Map. It can only be traversed to value, but it is difficult to traverse to key, so it is generally not necessary. If we only need to get the value value, it is more efficient to use values to traverse.

4. From the time-consuming results of for loop traversal and Iterator traversal in the above ways, the efficiency of Iterator traversal is a little faster than that of for loop.

The above is all the contents of the article "what is the Map Collection in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report