js實現Map功能

2022-08-28 18:48:08 字數 1645 閱讀 2379

/** map物件,實現map功能

** 介面:

* size() 獲取map元素個數

* isempty() 判斷map是否為空

* clear() 刪除map所有元素

* put(key, value) 向map中增加元素(key, value)

* remove(key) 刪除指定key的元素,成功返回true,失敗返回false

* get(key) 獲取指定key的元素值value,失敗返回null

* element(index) 獲取指定索引的元素(使用element.key,element.value獲取key和value),失敗返回null

* containskey(key) 判斷map中是否含有指定key的元素

* containsvalue(value) 判斷map中是否含有指定value的元素

* values() 獲取map中所有value的陣列(array)

* keys() 獲取map中所有key的陣列(array)

** 例子:

* var map = new map();

** map.put("key", "value");

* var val = map.get("key")

* ……**/

function map() ;

//判斷map是否為空

this.isempty = function() ;

//刪除map所有元素

this.clear = function() ;

//向map中增加元素(key, value)

this.put = function(_key, _value) );

};//刪除指定key的元素,成功返回true,失敗返回false

this.remove = function(_key)

}} catch (e)

return bln;

};//獲取指定key的元素值value,失敗返回null

this.get = function(_key)

}} catch (e)

};//獲取指定索引的元素(使用element.key,element.value獲取key和value),失敗返回null

this.element = function(_index)

return this.elements[_index];

};//判斷map中是否含有指定key的元素

this.containskey = function(_key)

}} catch (e)

return bln;

};//判斷map中是否含有指定value的元素

this.containsvalue = function(_value)

}} catch (e)

return bln;

};//獲取map中所有value的陣列(array)

this.values = function()

return arr;

};//獲取map中所有key的陣列(array)

this.keys = function()

return arr;};}

js實現map功能

var map function this.arrlength 0 假如有重複key,則不存入 this.put function key,value this.get function key 傳入的引數必須為map結構 this.putall function map else this.rem...

JS實現Map集合

function map this.put function key,value this.arr this.arr.length new struct key,value this.get function key return null this.values function return v...

map實現排序功能

map內部是按照hash演算法儲存的,但如果能對map排序在某些時候還是有用的 param h return 實現對map按照value公升序排序 suppresswarnings unchecked public static map.entry getsortedhashtablebyvalue...