你的位置:首页 > Java教程

[Java教程]javascript实现Map


 1 function Map() { 2   this.elements = new Array(); 3  4   // 获取MAP元素个数 5   this.size = function() { 6     return this.elements.length; 7   } 8  9   // 判断MAP是否为空 10   this.isEmpty = function() { 11     return (this.elements.length < 1); 12   } 13  14   // 删除MAP所有元素 15   this.clear = function() { 16     this.elements = new Array(); 17   } 18  19   // 向MAP中增加唯一元素(key, value) 20   this.put = function(_key, _value) { 21     this.remove(_key); 22     this.elements.push({ 23           key : _key, 24           value : _value 25         }); 26   } 27  28   // 向MAP中增加重复元素(key, value) 29   this.putRepeat = function(_key, _value) { 30     this.elements.push({ 31           key : _key, 32           value : _value 33         }); 34   } 35  36   // 删除指定KEY的元素,成功返回True,失败返回False 37   this.remove = function(_key) { 38     var bln = false; 39     try { 40       for (i = 0; i < this.elements.length; i++) { 41         if (this.elements[i].key == _key) { 42           this.elements.splice(i, 1); 43           return true; 44         } 45       } 46     } catch (e) { 47       bln = false; 48     } 49     return bln; 50   } 51  52   // 获取指定KEY的元素值VALUE,失败返回NULL 53   this.get = function(_key) { 54     try { 55       var result = null; 56       for (i = 0; i < this.elements.length; i++) { 57         if (this.elements[i].key == _key) { 58           result = this.elements[i].value; 59         } 60       } 61       return result; 62     } catch (e) { 63       return null; 64     } 65   } 66  67   // 设置MAP中指定KEY元素的值VALUE, 失败返回NULL 68   this.set = function(_key, _value) { 69     try { 70       this.remove(_key); 71       this.put(_key, _value); 72     } catch (e) { 73       return null; 74     } 75   } 76  77   // 获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL 78   this.element = function(_index) { 79     if (_index < 0 || _index >= this.elements.length) { 80       return null; 81     } 82     return this.elements[_index]; 83   } 84  85   // 判断MAP中是否含有指定KEY的元素 86   this.containsKey = function(_key) { 87     var bln = false; 88     try { 89       for (i = 0; i < this.elements.length; i++) { 90         if (this.elements[i].key == _key) { 91           bln = true; 92         } 93       } 94     } catch (e) { 95       bln = false; 96     } 97     return bln; 98   } 99 100   // 判断MAP中是否含有指定VALUE的元素101   this.containsValue = function(_value) {102     var bln = false;103     try {104       for (i = 0; i < this.elements.length; i++) {105         if (this.elements[i].value == _value) {106           bln = true;107         }108       }109     } catch (e) {110       bln = false;111     }112     return bln;113   }114 115   // 获取MAP中所有VALUE的数组(ARRAY)116   this.values = function() {117     var arr = new Array();118     for (i = 0; i < this.elements.length; i++) {119       arr.push(this.elements[i].value);120     }121     return arr;122   }123 124   // 获取MAP中所有KEY的数组(ARRAY)125   this.keys = function() {126     var arr = new Array();127     for (i = 0; i < this.elements.length; i++) {128       arr.push(this.elements[i].key);129     }130     return arr;131   }132 }