JavaScript Simulation HashMap class

xiaoxiao2021-03-06  99

Use JavaScript to simulate a HashMap class similar to Java

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------ Added by lxcjie 2004.7.16 * Simulation Simple HashMap class, require Key to be string, value can Yes, any type * method list: * 1, hashmap (): constructor * 2, PUT (key, value): void * 3, get (key): Object * 4, keyset (): array * 5, values ​​() : Array * 6, size (): int * 7, clear (): void * 8, iSempty (): Boolean * 9, Containskey (key): Boolean * 10, ContainSValue: Boolean // If Value is Object Type or array type, there is bug * 11, putall (map): void * 12, remove (key): void * Usage: * Var map = new jhashmap (); * map.put ("one", "a small small Pig "); * Map.Put (" Two "," two small pigs "); * map.put (" Three "," three small pigs "); * * whom (" [toString ()]: " map); * Print ("[get ()]:" map.get ("one")); * print ("[" [keyset ()]: " map.keyset ()); * Print (" [" VALUES ()]: " map.values ​​()); * print (" [size ()]: " map.size ()); * Print (" [iSempty ()]: " map.isempty () ); * Print ("[ContainSKey ()]:" map.containskey ("one")) * Print ("[ContaSValue ()]:" map.containsvalue ("Three Small Pigs")); * * // Putall * Var MapTemp = New JhashMap (); * MapTemp.Put ("Four", " Four small pigs "); * MapTemp.Put (" Five "," five small pigs "); * map.putall (mapTemp); * Print (" ["[putall ()]:" map); * * / / remove * map.remove ("two"); * map.remove ("one"); * print ("[transove ()]:" map); * * // clear * map.clear () * Print ("[Clear ()]:" map.size ());

* // Auxiliary method * function print (msg) * {* document.write (msg ""); *} * Modify resume: -------------------- -------------------------------------------------- --------- * // ** * HashMap constructor * / function jhashmap () {this.Length = 0; this.prefix = "hashmap_prefix_20040716 _";} / ** * Add key value to HashMap * / Jhashmap.prototype.put = function (key, value) {this [this.prefix key] = value; this.Length ;} / ** * Get the value value * / jhashmap.prototype from HashMap. Get = function (key) {return TypeOf this [this.prefix key] == "undefined"? null: this [this.prefix key];} / ** * Get all KEY collections from HashMap, with arrays Form Return * / jhashmap.prototype.keyset = function () {var arrkeyset = new array (); var index = 0; for (Var strkey in this) {if (strkey.substring (0, this.prefix.length) = = this.prefix) ArrkeySet [index ] = strkey.substring (this.prefix.Length);} return arrkeyset.length == 0? null: arrkeyset;} / ** * Get a collection of Value from HashMap, Array form Back * / jhashmap.prototype.values ​​= functioni ON () {var arrvalues ​​= new array (); var index = 0; for (Var strkey in this) {if (strkey.substring (0, this.prefix.length) == this.prefix) Arrvalues ​​[Index ] = this [strkey];} return arrvalues.length == 0? null: arrvalues;} / ** * Gets the number of value values ​​of HashMap * / jhashmap.prototype.size = function () {Return this.Length;} / ** * Delete the specified value * / jhashmap.prototype.remove = function (key) {delete this [this.prefix key]; this.Length -;} / ** * Clear HashMap * / jhashmap.prototype.clear =

转载请注明原文地址:https://www.9cbs.com/read-124318.html

New Post(0)