• JS 实现 HashMap


    HashMap代码(这种实现方式是错误的,错误原因:代码中_map、_length变量是HashMap的所有实例共用的):

    /**
     * HashMap
     * 2021年09月09日
     */
    
    (function (global) {
        var _map;
        var _length;
    
        global.HashMap = function () {
            _map = {};
            _length = 0;
        };
    
        global.HashMap.prototype = {
            put: function (key, value) {
                if (!_map.hasOwnProperty(key)) {
                    _length++;
                }
                _map[key] = value;
            },
    
            get: function (key) {
                if (_map.hasOwnProperty(key)) {
                    return _map[key];
                }
                return null;
            },
    
            containsKey: function (key) {
                return _map.hasOwnProperty(key);
            },
    
            size: function () {
                return _length;
            },
    
            remove: function (key) {
                if (_map.hasOwnProperty(key)) {
                    _length--;
                    return delete _map[key];
                }
                return false;
            },
    
            removeAll: function () {
                _map = {};
            },
    
            keys: function () {
                var keys = [];
                for (var item in _map) {
                    keys.push(item);
                }
                return keys;
            },
    
            values: function () {
                var values = [];
                for (var item in _map) {
                    values.push(_map[item]);
                }
                return values;
            }
        };
    
        global.HashMap.prototype.constructor = global.HashMap;
    
    })(window);
    View Code

    HashMap代码(正确的实现):

    /**
     * HashMap
     * 2021年09月09日
     */
    
    HashMap = function () {
        this._length = 0;
        this._map = {};
    };
    
    HashMap.prototype = {
        put: function (key, value) {
            if (!this._map.hasOwnProperty(key)) {
                this._length++;
            }
            this._map[key] = value;
        },
    
        get: function (key) {
            if (this._map.hasOwnProperty(key)) {
                return this._map[key];
            }
            return null;
        },
    
        containsKey: function (key) {
            return this._map.hasOwnProperty(key);
        },
    
        size: function () {
            return this._length;
        },
    
        remove: function (key) {
            if (this._map.hasOwnProperty(key)) {
                this._length--;
                return delete this._map[key];
            }
            return false;
        },
    
        removeAll: function () {
            this._length = 0;
            this._map = {};
        },
    
        keys: function () {
            var keys = [];
            for (var item in this._map) {
                keys.push(item);
            }
            return keys;
        },
    
        values: function () {
            var values = [];
            for (var item in this._map) {
                values.push(this._map[item]);
            }
            return values;
        }
    };
    
    HashMap.prototype.constructor = HashMap;
    View Code

    测试代码:

    <!DOCTYPE html>
    <html>
    <head>
        <title>HashMap测试</title>
    
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <style type="text/css">
        </style>
    
        <script type="text/javascript" src="jquery-1.7.1.js"></script>
        <script type="text/javascript" src="hashmap.js"></script>
    
    </head>
    <body>
        <input type="button" value="测试1" onclick="testHashMap()" />
        <input type="button" value="测试2" onclick="testArray()" />
        <div id="div" style="height:800px;"></div>
    
        <script type="text/javascript">
            var n = 100000;
            var div = $("#div");
    
            //测试数据
            debugger;
            var list = [];
            var map = new HashMap();
            for (var i = 1; i <= n; i++) {
                var val = Math.floor(Math.random() * n);
                var key = "key" + val;
    
                if (!map.containsKey(key)) {
                    map.put(key, val);
                    list.push({ key: key, value: val });
                }
            }
            div.append("测试数据初始化完成,数据量:Array " + list.length + ",HashMap " + map.size() + "<br />");
    
            //测试HashMap
            function testHashMap() {
                var t1 = new Date().getTime();
    
                var sum = 0;
                var count = 0;
                for (var i = 20000; i <= 21000; i++) {
                    var key = "key" + i;
    
                    if (map.containsKey(key)) {
                        var num = map.get(key);
                        count++;
                        sum += num;
                    }
                }
    
                var t2 = new Date().getTime();
    
                div.append("完成,结果:sum=" + sum + ",count=" + count + "耗时" + (t2 - t1).toString() + "毫秒<br />");
            }
    
            //测试Array
            function testArray() {
                var t1 = new Date().getTime();
    
                var sum = 0;
                var count = 0;
                for (var i = 20000; i <= 21000; i++) {
                    for (var k = 0; k < list.length; k++) {
                        var key = "key" + i;
                        var item = list[k];
    
                        if (item.key == key) {
                            count++;
                            sum += item.value;
                        }
                    }
                }
    
                var t2 = new Date().getTime();
    
                div.append("完成,结果:sum=" + sum + ",count=" + count + "耗时" + (t2 - t1).toString() + "毫秒<br />");
            }
    
        </script>
    </body>
    </html>
    View Code
  • 相关阅读:
    js常用函数(不断添加中。。。)
    【转】linux常用命令全集
    【转】VC++消息钩子编程
    beyond compare ftp 文件夹同步
    【转】notepad++ 应用学习 -- 列模式,十六进制模式
    网页嵌入视频播放器
    java中 正则表达式的使用
    【转】正则表达式30分钟入门教程
    【转】深入研究java.lang.Runtime类
    js 中map的几种实现方式
  • 原文地址:https://www.cnblogs.com/s0611163/p/15246905.html
Copyright © 2020-2023  润新知