复制代码 代码如下: <br /> function HashMap() {<br /> /**Map大小**/<br /> var size = 0;<br /> /**对象**/<br /> var entry = new Object();<br /> /**Map的存put方法**/<br /> this.put = function(key, value) {<br /> if (!this.containsKey(key)) {<br /> size++;<br /> entry[key] = value;<br /> }<br /> }<br /> /**Map取get方法**/<br /> this.get = function(key) {<br /> return this.containsKey(key) ? entry[key] : null;<br /> }<br /> /**Map删除remove方法**/<br /> this.remove = function(key) {<br /> if (this.containsKey(key) && (delete entry[key])) {<br /> size--;<br /> }<br /> }<br /> /**是否包含Key**/<br /> this.containsKey = function(key) {<br /> return (key in entry);<br /> }<br /> /**是否包含Value**/<br /> this.containsValue = function(value) {<br /> for (var prop in entry) {<br /> if (entry[prop] == value) {<br /> return true;<br /> }<br /> }<br /> return false;<br /> }<br /> /**所有的Value**/<br /> this.values = function() {<br /> var values = new Array();<br /> for (var prop in entry) {<br /> values.push(entry[prop]);<br /> }<br /> return values;<br /> }<br /> /**所有的 Key**/<br /> this.keys = function() {<br /> var keys = new Array();<br /> for (var prop in entry) {<br /> keys.push(prop);<br /> }<br /> return keys;<br /> }<br /> /**Map size**/<br /> this.size = function() {<br /> return size;<br /> }<br /> /**清空Map**/<br /> this.clear = function() {<br /> size = 0;<br /> entry = new Object();<br /> } <p>}<br /> //创建HashMap对象 <br /> var hashMap = new HashMap();<br /> hashMap.put("A", "1");<br /> hashMap.put("B", "2");<br /> hashMap.put("A", "5");<br /> hashMap.put("C", "3");<br /> hashMap.put("A", "4");<br /> alert(hashMap.size()); <p>