javascript之HashMap
用法
var map = new HashMap(); map.put("a",1); map.put("b",1); map.put("b",1); alert(map.get("a")); alert(map.keys().toString()); alert(map.values.toString()); alert(map.size()); map.remove("a"); alert(map.keys().toString());
代码
/* jquery Map 对象 @desc:基于js的hashmap */ function HashMap() { /** Map 大小 * */ var size = 0; /** 对象 * */ var entry = new Object(); /** 存 * */ this.put = function(key, value) { if (!this.containsKey(key)) { size++; } entry[key] = value; } /** 取 * */ this.get = function(key) { if (this.containsKey(key)) { return entry[key]; } else { return null; } } /** 删除 * */ this.remove = function(key) { if (delete entry[key]) { size--; } } /** 是否包含 Key * */ this.containsKey = function(key) { return (key in entry); } /** 是否包含 Value * */ this.containsValue = function(value) { for ( var prop in entry) { if (entry[prop] == value) { return true; } } return false; } /** 所有 Value * */ this.values = function() { var values = new Array(); for ( var prop in entry) { values.push(ent); } return values; } /** 所有 Key * */ this.keys = function() { var keys = new Array(); for ( var prop in entry) { var ent = entry[prop]; if (ent != null && ent != undefined && ent.length > 0) keys.push(ent); } return keys; } /** Map Size * */ this.size = function() { return size; } /** **移除map所有信息** */ this.removeALl = function() { var str_key = this.keys(); for (var i = 0; i < str_key.length; i++) { if (null != str_key[i] && "" != str_key[i]) { this.remove(str_key[i]); } } } } /** * * 字符串拼接 QUINN * */ function StringBuffer() { this._strs = new Array(); } StringBuffer.prototype.append = function(str) { this._strs.push(str); }; StringBuffer.prototype.arrayToString = function() { return this._strs.join(""); };
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22