Javascript 中 HashMap 实现
来源:互联网 发布:沈飞 知乎 编辑:程序博客网 时间:2024/06/06 13:21
诸如HashMap、Stack、Queue都可以通过操作JS数组进行实现。下面我们首先参照Javascript官方使用手册对JS Array对象中的一些常用函数进行解释一下,以便进行下面的数据结构的实现(JS Array对象类似于堆栈的元素进出栈顺序):
Push : 添加一个元素
Reverse:将数组元素进行逆序
Shift:移除数组中第一个元素(下标为0)
Push : 添加一个元素
Reverse:将数组元素进行逆序
Shift:移除数组中第一个元素(下标为0)
Pop:弹出最后一个元素(下标为array.length-1)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> New Document </title> <script type="text/javascript"> function HashMap(){ this.source = new Array(); this.length = 0; }; /*将函数列表(实为JSON数据)映射到HashMap的prototype属性中*/ function Apply(o, c){ if(o && c && typeof c == 'object'){for(var p in c){o[p] = c[p];}}return o; }; Apply(HashMap.prototype,{ /*获取元素的个数*/size : function(){ return this.length },/*查看是否存在该key值*/containsKey :function (key) { if(this.source[key]) return true;else return false; }, /*查看是否存在该value值*/containsValue :function (value) { for (var key in this.source) { if (this.source[key] == value) { return true; } } return false; }, /*存入数据 返回值如果为false表示该key值已经存过数据,我们会替换前一条数据*/put : function(key,value) { var tag = this.containsKey(key); this.length += ((tag)? 0: 1); this.source[key] = value; return !tag; }, /*根据key读取数据*/ get : function(key) { return this.source[key]; }, /* 根据key值删除HashMap中元素,返回删除元素的value */ remove : function(key) { var value = this.source[key]; if( this.containsKey(key) && ( delete this.source[key] ) ) { this.length --; } return value; }, /*清除所有数据*/ clear : function() { delete this.source; this.source = new Array(); this.length = 0; return 0; }, obj2str :function(o) { var r = []; if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\""; if (typeof o == "object") { for (var i in o) r.push("\"" + i + "\":" + this.obj2str(o[i])); if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) { r.push("toString:" + o.toString.toString()); } r = "{" + r.join() + "}"; return r; } return o.toString(); }, /*将数据以JSON字串形式输出*/ toString : function() { return this.obj2str(this.source); } }); var hsp = new HashMap(); document.writeln(hsp.put('a',"apple")); //true document.writeln(hsp.put('b',"boy")); //true document.writeln(hsp.put('c',"cat")); //true document.writeln(hsp.put('a',"replaceApple")); //false document.writeln(hsp.get("a")); //"replaceApple" document.writeln(hsp.size()); //3 document.writeln(hsp.containsValue('cat')); //true document.writeln(hsp.containsKey('a')); //true document.writeln(hsp.remove('a')); //"replaceApple" document.writeln(hsp.toString()); //"{"b":"boy","c":"cat"}" document.writeln(hsp.clear()); //2 document.writeln(hsp.toString()); "{}" </script> </head> <body> </body></html>
注:Stack和Queue的实现可以参看: http://blog.csdn.net/wdxgdiy/article/details/8877565 写的不错
附件下载 : http://pan.baidu.com/share/link?shareid=3844618167&uk=1763003608
- Javascript 中 HashMap 实现
- javascript 实现 HashMap
- javascript实现hashmap
- 【JavaScript】JS实现HashMap
- Javascript实现HashMap
- javascript实现java的HashMap
- HashMap的JavaScript简洁实现
- java中HashMap实现原理
- java中HashMap的实现
- Java中HashMap底层实现
- 1.8 中hashMap的实现
- javascript实现java中的HashMap的功能
- Javascript实现Java的HashMap(链表散列)
- JavaScript中简洁实用的HashMap对象
- Java中HashMap详解 - HashMap源码及实现原理
- HashMap中put与get的实现
- java中HashMap的实现方式
- Java中HashMap的实现原理
- 51单片机以定时中断的方法实现DS18B20时序
- 【处理空格】poj2503——Babelfish
- hdu 4405 概率DP
- Linux下I2C设备驱动开发和实现
- 短信联系人URI
- Javascript 中 HashMap 实现
- 泰山挑夫2
- 51
- 第十五章 Linux I2C核心、总线与设备驱动
- handle与HWND的差别
- MyEclipse-2013/专业版/spring/blue的破解过程
- CodeForces 15C. Industrial Nim
- c++中的存储类型
- 校正控制器(传递函数)转化为算法的简单描述