javascript實現(xiàn)的HashMap類代碼
更新時間:2014年06月27日 10:19:27 投稿:junjie
這篇文章主要介紹了javascript實現(xiàn)的HashMap類代碼,實現(xiàn)了添加、獲取、刪除、查詢key和value功能,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<script language = "javascript" >
function HashMap() {
/**Map大小**/
var size = 0;
/**對象**/
var entry = new Object();
/**Map的存put方法**/
this.put = function(key, value) {
if (!this.containsKey(key)) {
size++;
entry[key] = value;
}
}
/**Map取get方法**/
this.get = function(key) {
return this.containsKey(key) ? entry[key] : null;
}
/**Map刪除remove方法**/
this.remove = function(key) {
if (this.containsKey(key) && (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(entry[prop]);
}
return values;
}
/**所有的 Key**/
this.keys = function() {
var keys = new Array();
for (var prop in entry) {
keys.push(prop);
}
return keys;
}
/**Map size**/
this.size = function() {
return size;
}
/**清空Map**/
this.clear = function() {
size = 0;
entry = new Object();
}
}
//創(chuàng)建HashMap對象
var hashMap = new HashMap();
hashMap.put("A", "1");
hashMap.put("B", "2");
hashMap.put("A", "5");
hashMap.put("C", "3");
hashMap.put("A", "4");
alert(hashMap.size());
</script>
您可能感興趣的文章:
相關(guān)文章
javascript中select下拉框的用法總結(jié)
這篇文章主要為大家介紹了javascript中select下拉框的用法,select在開發(fā)中經(jīng)常被用到,用于進行選項選擇,需要的朋友可以參考下2016-01-01使用JavaScript實現(xiàn)連續(xù)滾動字幕效果的方法
這篇文章主要介紹了使用JavaScript實現(xiàn)連續(xù)滾動字幕效果的方法,文中給出了瀏覽器端運行的示例腳本,需要的朋友可以參考下2015-07-07e.target與e.currentTarget對象的使用區(qū)別詳解
這篇文章主要為大家介紹了e.target與e.currentTarget的使用區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07