JavaScript Implementation Map Collection

Source: Internet
Author: User

Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106-107 var Map = function () {/************ base variable **************/var hashmap = {}; var keys = [] ; var vals = []; var entrys = []; var size = 0; var index = {};   var Entry = function (key,value) {var entrykey = key; var entryvalue = value; this.getkey = function () {return entr Ykey; }; This.getvalue = function () {return entryvalue;}; }; /************ Basic Methods Alphabetically **************/This.clear = function (key) {Hashmap[key] = undefined; var i = Index[key]; entrys.s Plice (i,1); Vals.splice (i,1); Keys.splice (i,1); Size--; };   This.entryset = function () {return entrys;};   This.get = function (key) {return hashmap[key];   This.isempty = function () {if (HashMap) return true;   This.keyset = function () {return keys;};   This.put = function (key,value) {if (!this.get (key)) {Entrys.push (new Entry (Key,value)); Keys.push (key); Vals.push (value); Index[key] = size; Size + +; else {var i = Index[key]; entrys[i] = new Entry (key, value); Vals[i] = value; } Hashmap[key] = value; };   this.size = function () {return size;};   this.values = function () {return vals;}; }; /************ extension method **************/Map.prototype = {containskey:function (key) {if (This.get (key)) return true; return FAL Se }, Putall:function (set) {for (Var e in set) {if (Set[e]) {this.put (e,set[e]);}}, Remove:function (key) {var v = thi S.get (key); This.clear (key); return v; } }; var h = new Map (); H.put (' A ', 10); H.put (' B ', 11); H.put (' C ', 3); H.put (' d ', 5); Console.info (H.size ()); H.clear (' a '); Console.info (H.containskey (' a ')); Console.info (H.containskey (' B ')); Console.info (H.size ());   Console.log (H.entryset ()); Console.log (H.keyset ()); Console.log (H.values ());   for (Var i in H.entryset ()) {var obj = H.entryset () [i]; Console.log (Obj.getkey () + ":" + obj.getvalue ());}  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.