javascript實(shí)現(xiàn)的HashMap類(lèi)代碼
<script language = "javascript" >
function HashMap() {
/**Map大小**/
var size = 0;
/**對(duì)象**/
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對(duì)象
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>
- Java中HashMap和TreeMap的區(qū)別深入理解
- JAVA HashMap詳細(xì)介紹和示例
- 解析WeakHashMap與HashMap的區(qū)別詳解
- 淺析Java中Map與HashMap,Hashtable,HashSet的區(qū)別
- java HashMap通過(guò)value反查key的代碼示例
- Android中實(shí)現(xiàn)HashMap排序的方法
- Java中HashMap和Hashtable及HashSet的區(qū)別
- java中Hashtable和HashMap的區(qū)別分析
- java遍歷HashMap簡(jiǎn)單的方法
- jdk7 中HashMap的知識(shí)點(diǎn)總結(jié)
相關(guān)文章
javascript中select下拉框的用法總結(jié)
這篇文章主要為大家介紹了javascript中select下拉框的用法,select在開(kāi)發(fā)中經(jīng)常被用到,用于進(jìn)行選項(xiàng)選擇,需要的朋友可以參考下2016-01-01基于javascript滾動(dòng)圖片具體實(shí)現(xiàn)
這篇文章主要介紹了javascript滾動(dòng)圖片具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11使用JavaScript實(shí)現(xiàn)連續(xù)滾動(dòng)字幕效果的方法
這篇文章主要介紹了使用JavaScript實(shí)現(xiàn)連續(xù)滾動(dòng)字幕效果的方法,文中給出了瀏覽器端運(yùn)行的示例腳本,需要的朋友可以參考下2015-07-07e.target與e.currentTarget對(duì)象的使用區(qū)別詳解
這篇文章主要為大家介紹了e.target與e.currentTarget的使用區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07