本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接博客园蜗牛 cnblogs.com\tdws .

首先提供一种获取hashCode的方法,是一种比较受欢迎的方式,该方法参照了一位园友的文章,链接在尾部给出:

 var djb2Code = function (str) {
var hash = 5381;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
}
return hash;
}

接下来我们用js实现hashmap, hashmap是一种键值对的数据结构。意味着你可以通过key快速找到你所需要查找的值。我使用数组加上LinkedList来实现hashmap,这种方式也被称为解决hashcode冲突的分离链接法。hashmap通常具备以下几种方法:put,get,remove。put是写入和修改数据,在put数据时,首先获取key的hashcode,作为数组的索引。而数组索引对应的值则是一个linkedlist,并且linkedlist所存储的节点值,同时包含着所需存储的key和value。这样以便解决当hashcode重复冲突时,在链表中根据key名称来get查找值。 关于hashmap更多的原理,我推荐这篇文章 http://www.admin10000.com/document/3322.html

下面直接给出实现,其中使用到LinkedList数据结构的源码,在我的这篇分享当中:http://www.cnblogs.com/tdws/p/6033209.html

   var djb2Code = function (str) {
var hash = 5381;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
}
return hash;
} function HashMap() {
var map = [];
var keyValPair = function (key, value) {
this.key = key;
this.value = value;
}
this.put = function (key, value) {
var position = djb2Code(key);
if (map[position] == undefined) {
map[position] = new LinkedList();
}
map[position].append(new keyValPair(key, value));
},
this.get = function (key) {
var position = djb2Code(key);
if (map[position] != undefined) {
var current = map[position].getHead();
while (current.next) {
if (current.element.key === key) { //严格判断
return current.element.value;
}
current = current.next;
}
if (current.element.key === key) {//如果只有head节点,则不会进while. 还有尾节点,不会进while,这个判断必不可少
return current.element.value;
}
}
return undefined;
},
this.remove = function (key) {
var position = djb2Code(key);
if (map[position] != undefined) {
var current = map[position].getHead();
while (current.next) {
if (current.element.key === key) {
map[position].remove(current.element);
if (map[position].isEmpty()) {
map[position] == undefined;
}
return true;
}
current = current.next;
}
if (current.element.key === key) {
map[position].remove(current.element);
if (map[position].isEmpty()) {
map[position] == undefined;
}
return true;
}
}
}
} //链表
function LinkedList() {
var Node = function (element) {        //新元素构造
this.element = element;
this.next = null;
};
var length = 0;
var head = null; this.append = function (element) {
var node = new Node(element);        //构造新的元素节点
var current;
if (head === null) {             //头节点为空时 当前结点作为头节点
head = node;
} else {
current = head;
while (current.next) {          //遍历,直到节点的next为null时停止循环,当前节点为尾节点
current = current.next;
}
current.next = node;            //将尾节点指向新的元素,新元素作为尾节点
}
length++;                    //更新链表长度
};
this.removeAt = function (position) {
if (position > -1 && position < length) {
var current = head;
var index = 0;
var previous;
if (position == 0) {
head = current.next;
} else {
while (index++ < position) {
previous = current;
current = current.next;
}
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
this.insert = function (position, element) {
if (position > -1 && position <= length) {        //校验边界
var node = new Node(element);
current = head;
var index = 0;
var previous;
if (position == 0) {                    //作为头节点,将新节点的next指向原有的头节点。
node.next = current;
head = node;                        //新节点赋值给头节点
} else {
while (index++ < position) {
previous = current;
current = current.next;
}                                //遍历结束得到当前position所在的current节点,和上一个节点
previous.next = node;                    //上一个节点的next指向新节点 新节点指向当前结点,可以参照上图来看
node.next = current;
}
length++;
return true;
} else {
return false;
} };
this.toString = function () {
var current = head;
var string = '';
while (current) {
string += ',' + current.element;
current = current.next;
}
return string;
};
this.indexOf = function (element) {
var current = head;
var index = -1;
while (current) {
if (element === current.element) {            //从头节点开始遍历
return index;
}
index++;
current = current.next;
}
return -1;
};
this.getLength = function () {
return length;
};
this.getHead = function () {
return head;
};
this.isEmpty = function () {
return length == 0;
}
}

参考文章:js获取hashcode  : http://www.cnblogs.com/pigtail/p/3342977.html

如果我的点滴分享对你有点滴帮助,欢迎点击下方红色按钮,我将长期输出分享。

JavaScript——HashMap实现的更多相关文章

  1. JavaScript资源大全中文版(Awesome最新版)

    Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...

  2. 【转】 我的java web登录RSA加密

    [转] 我的java web登录RSA加密 之前一直没关注过web应用登录密码加密的问题,这两天用appscan扫描应用,最严重的问题就是这个了,提示我明文发送密码.这个的确很不安全,以前也大概想过, ...

  3. JavaScript 模拟 HashMap例子

    function map(){    var map = {}; // Map map = new HashMap();        var key = "key";    va ...

  4. JavaScript: 零基础轻松学闭包

    本文面向初学者,大神轻喷. 闭包是什么? 初学javascript的人,都会接触到一个东西叫做闭包,听起来感觉很高大上的.网上也有各种五花八门的解释,其实我个人感觉,没必要用太理论化的观念来看待闭包. ...

  5. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  6. JavaScript资源大全中文版(Awesome最新版--转载自张果老师博客)

    JavaScript资源大全中文版(Awesome最新版)   目录 前端MVC 框架和库 包管理器 加载器 打包工具 测试框架 框架 断言 覆盖率 运行器 QA 工具 基于 Node 的 CMS 框 ...

  7. 【JavaScript】详解JSON

    目录结构: // contents structure [-] 什么是JSON JSON和XML的比较 相同点 不同点 JSON语法 如何解析JSON文本 eval()方法 JSON.parse()方 ...

  8. 重新介绍 JavaScript

    简介 为什么需要这个重新介绍呢?因为 JavaScript 已经完全可以被称为世界上被误解最严重的编程语言了.虽然它被当做玩具来用,但是藏在让人迷惑的简单表象下面的,是强大的语言特性.从2005年,一 ...

  9. 【javascript】:Highcharts实战

    PS: Highcharts是一款前端图标设计框架,非常绚. 前端JS: var probabilityStatisticsData; var yearTool; var CoordinateX = ...

随机推荐

  1. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  2. HDU 4280 Island Transport(网络流,最大流)

    HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...

  3. cygwin简介及使用

    一个是真实的假货,一个是冒牌的真品前指 Cygwin,后指 Linux/VMWare 路不管平还是陡,终归你要走的,如果你愿意投入到linux开发的社群中来,不会安装linux系统,不会配置工作环境是 ...

  4. Java流程控制---个人参考资料

    前言:我写博客的目的很简单,很单纯,把自己平时学的东西,放到博客上,空闲的时间,就可以看看自己曾经看到过得东西. Java流程控制语句:判断结构.选择结构.循环结构 一.判断结构 判断结构包括if 分 ...

  5. os.chmod()--更改目录授权权限

    用法:os.chmod() 方法用于更改文件或目录的权限. 语法:os.chmod(path, mode) 参数:只需要2个参数,一个是路径,一个是说明路径的模式. path -- 文件名路径或目录路 ...

  6. node爬虫进阶版

    手写了一个方便爬虫的小库: const url = require('url') const glib = require('zlib') //默认头部 const _default_headers ...

  7. 鸟哥的Linux私房菜——第九章

    视频链接,推荐看B站 土豆网:http://www.tudou.com/programs/view/XmMDbjJHJC8 B站:http://www.bilibili.com/video/av966 ...

  8. service注入到action中

    service注入到action中 之前本人每次要获得service都是在action自己通过WebApplicationContext的getBean获得的,一直在spring中只配置到了servi ...

  9. SQL记录-PLSQL变量与常量文字

    PL/SQL变量   变量是只不过是一个给定的存储区域,程序可以操纵的名称.PL/SQL每个变量具有一个特定的数据类型,它决定了大小和变量的存储器的值,可以说存储器和设置操作可以施加到可变内被存储的范 ...

  10. Spring RedisTemplate操作-HyperLogLog操作(7)

    @Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...