In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the principle of ES6 Map". In daily operation, I believe that many people have doubts about what the principle of ES6 Map is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the principle of ES6 Map?" Next, please follow the editor to study!
Map is realized by using linked list and hash.
First of all, Map can delete, and the deleted data can be an intermediate value. The advantage of the linked list is to add anywhere in the middle, delete elements very quickly, do not need to move other elements, directly change the pointer point.
In the case of storing a lot of data, the chain will be too long, resulting in slow search efficiency, so we can create a bucket (container for storing objects) and allocate data evenly according to hash (changing the value of the hash to a fixed value through the algorithm) to prevent the chain from being too long.
As shown in the following figure: there are three positions in the bucket, each of which is an object. Connect the unassociated objects together by pointing to the next object through the next attribute.
Store both the Map property value and the property name in the value of the object.
Simply simulate Map. The code is as follows:
The function Mymap () {/ / constructor this.init ();} / / initializes the function, creates a bucket (array), each position is an object, sets the next property on each object's property, and initializes it to null. Mymap.prototype.init = function () {this.tong = new Array (8); for (var I = 0; I < 8; iSum +) {this.tong [I] = new Object (); this.data [I] .next = null;}}; / / add data. Mymap.prototype.set = function (key, value) {var index = this.hash (key); / / get the currently set key setting to that location var TempBucket = this.tong [index] / / get the current location of the object while (TempBucket.next) {/ / traverse if the next if of the current object link is not empty (TempBucket.next.key = = key) {/ / if the property to be set already exists, overwrite its value. TempBucket.next.value = value; return; / / return, stop traversing} else {TempBucket = TempBucket.next; / / and point the pointer to the next object. }} TempBucket.next = {/ / the next of the object is null, add the object. Key: key, value: value, next: null}}; / / query data Mymap.prototype.get = function (key) {var index = this.hash (key); var TempBucket = this.tong [index]; while (TempBucket) {if (TempBucket.key = = key) {return TempBucket.value;} else {TempBucket = TempBucket.next;} return undefined } / / delete data Mymap.prototype.delete = function (key) {var index = this.hash (key); var TempBucket = this.tong [index]; while (TempBucket) {if (TempBucket.next.key = = key) {TempBucket.next = TempBucket.next.next; return true;} else {TempBucket = TempBucket.next } / / check whether the current attribute exists Mymap.prototype.has = function (key) {var index = this.hash (key); var TempBucket = this.tong [index]; while (TempBucket) {if (TempBucket.key = = key) {return true;} else {TempBucket = TempBucket.next;}} return false } / / clear the mapMymap.prototype.clear = function () {this.init ();} / so that the set properties are evenly distributed to each location so that a chain is not too long. Mymap.prototype.hash = function (key) {var index = 0; if (typeof key = = "string") {for (var I = 0; I < 3; iTunes +) {index = index + isNaN (key.charCodeAt (I))? 0: key.charCodeAt (I);}} else if (typeof key = = 'object') {index = 0 } else if (typeof key = = 'number') {index = isNaN (key)? 7: key;} else {index = 1;} return index% 8;} var map = new Mymap (); / / instantiate mapmap.set (' name','zwq'); map.get ('name'); map.has (' name) using the constructor At this point, the study of "what is the principle of ES6 Map" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.