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

Use HashMap to access key-value pairs in Java

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

Share

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

Today, I will talk to you about using HashMap to access key-value pairs in Java, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Map is one of the most commonly used data structures in daily programming. It retains key-value pairs that can be easily accessed through its keys. In Java, it is obvious to use HashMap for this purpose. However, in JavaScript, it is very convenient to use a normal object to achieve this goal.

Const map = {}; / / insert key value pair map ['key1'] =' value1'; map ['key2'] =' value2'; map ['key3'] =' value3'; / / check the key if (map.hasOwnProperty ('key1')) {console.log (' Map contains key1') contained in map;} / / obtain the value console.log (map ['key1']) through a specific key

But JavaScript has a built-in data structure dedicated to this purpose: Map. Let me give you some reasons to like Map rather than ordinary objects.

1. More key types

Objects can only have symbols (symbols) or strings. Map can use any type of value as a key: object, function, or primitive (primitives).

Const map = new Map (); const myFunction = () = > console.log ('I am a useful function.'); const myNumber = 666; const myObject = {name: 'plainObjectValue', otherKey:' otherValue'}; map.set (myFunction, 'function as a key'); map.set (myNumber,' number as a key'); map.set (myObject, 'object as a key'); console.log (map.get (myFunction)); / / function as a key console.log (map.get (myNumber)) / / number as a key console.log (map.get (myObject)); / / object as a key

two。 Better sizing

Map provides a size property, but the size of a normal object must be determined in a difficult way. Determining the size of a Map can be done in O (1) time, while determining the size of a normal object requires O (n) steps.

Const map = new Map (); map.set ('someKey1', 1); map.set (' someKey2', 1); Map.set ('someKey100', 1); console.log (map.size) / / 100, Runtime: O (1) const plainObjMap = {}; plainObjMap [' someKey1'] = 1; plainObjMap ['someKey2'] = 1; PlainObjMap ['someKey100'] = 1; console.log (Object.keys (plainObjMap) .length) / / 100, Runtime: O (n)

3. Better performanc

Map is optimized to add and delete entries frequently.

In addition, the number of entries in Map can be retrieved in a constant amount of time, while the number of entries in an ordinary object must be calculated, which takes O (n) time.

Take my Macbook Pro as an example, this is the average size determination time of a 10 million-entry Map.

Normal JS object: ~ 1.6s

Map: < 1 ms

In addition, it does not need to convert any keys to strings, which can save a lot of time.

4. Direct iteration

The object must get the key and iterate over it. Map, on the other hand, is iterable, which means it can iterate directly.

Const map = new Map (); map.set ('someKey1', 1); map.set (' someKey2', 2); map.set ('someKey3', 3); for (let [key, value] of map) {console.log (`${key} = ${value}`);} / / someKey1 = 1 / / someKey2 = 2 / / someKey3 = 3 const plainObjMap = {}; plainObjMap [' someKey1'] = 1; plainObjMap ['someKey2'] = 2; plainObjMap [' someKey3'] = 3 For (let key of Object.keys (plainObjMap)) {const value = plainObjMap [key]; console.log (`${key} = ${value}`);} / / someKey1 = 1 / / someKey2 = 2 / / someKey3 = 3

5.key sequence

Prior to ECMAScript 2015, the keys of an object were not guaranteed to appear in any particular order. Iterate on Map to ensure that the keys appear in the order in which they are inserted.

6. Keyless overlay

Because the prototype of a normal object already contains some keys, there may be conflicts between your keys and the keys that the object already contains. Map is created without any keys.

Note: since ECMAScript 2015, you can use Object.create (null) to create your normal object graph to avoid accidental key overrides.

Const map = new Map (); map.set ('someKey1', 1); map.set (' someKey2', 2); map.set ('toString', 3); / / No problem for Map const plainObjMap = new Map (); plainObjMap [' someKey1'] = 1; plainObjMap ['someKey2'] = 2; plainObjMap [' toString'] = 3; / / Oops, native property after reading the above, do you have any further understanding of the key-value pairs that use HashMap to achieve access in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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