javascript实现的HashMap类代码
<scriptlanguage="javascript"> functionHashMap(){ /**Map大小**/ varsize=0; /**对象**/ varentry=newObject(); /**Map的存put方法**/ this.put=function(key,value){ if(!this.containsKey(key)){ size++; entry[key]=value; } } /**Map取get方法**/ this.get=function(key){ returnthis.containsKey(key)?entry[key]:null; } /**Map删除remove方法**/ this.remove=function(key){ if(this.containsKey(key)&&(deleteentry[key])){ size--; } } /**是否包含Key**/ this.containsKey=function(key){ return(keyinentry); } /**是否包含Value**/ this.containsValue=function(value){ for(varpropinentry){ if(entry[prop]==value){ returntrue; } } returnfalse; } /**所有的Value**/ this.values=function(){ varvalues=newArray(); for(varpropinentry){ values.push(entry[prop]); } returnvalues; } /**所有的Key**/ this.keys=function(){ varkeys=newArray(); for(varpropinentry){ keys.push(prop); } returnkeys; } /**Mapsize**/ this.size=function(){ returnsize; } /**清空Map**/ this.clear=function(){ size=0; entry=newObject(); }
} //创建HashMap对象 varhashMap=newHashMap(); hashMap.put("A","1"); hashMap.put("B","2"); hashMap.put("A","5"); hashMap.put("C","3"); hashMap.put("A","4"); alert(hashMap.size());
</script>