Map set implemented by javascript and javascriptmap set

Source: Internet
Author: User

Map set implemented by javascript and javascriptmap set

A few days ago, the project wanted to use a map set of the same things. It took a while to get the object. Today, I am idle to implement it.
Don't laugh

Code

Var Map = function () {/************ basic 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 entryKey;}; this. getValue = function () {return entryValue ;};};. clear = function (key) {hashm Ap [key] = undefined; var I = index [key]; entrys. splice (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; return false ;}; 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 ;};};. prototype = {containsKey: function (key) {if (this. get (key) return true; return false ;}, putAll: function (set) {for (var e in set) {if (set [e]) {this. put (e, set [e]) ;}}, remove: function (key) {var v = this. 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 ());}

The above is all the content of this article. I hope you will like it.

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.