DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript實現的HashMap類代碼
javascript實現的HashMap類代碼
編輯:關於JavaScript     

復制代碼 代碼如下:
<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();
    }

}
//創建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>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved