对哈希表的研究

 

哈希其实就是js⾥的对象,它在实际的键值和存⼊的哈希值之间存在⼀层映射。如下例⼦:

 

 

 

class HashTable{

constructor() {
this.iems={}
}
put(key,value){
const hash=this.keyToHash(key)
this.items[hash]=value
}
get(key){
return this.items[this.keyToHash(key)]
}
remove(key){
delete (this.items[this.keyToBash(key)])
}
keyToHash(key){
let hash=0
for(let i=0;i<key.length;i++){
hash+=key.charCodeAt(i)
}
hash=hash%37
return hash
}
}
let kkb=new HashTable()
kkb.put("name","nihao")
kkb.put("age","6")
kkb.put("best","大胜老师")
console.log(kkb.get("name"))
console.log(kkb.get("best"))
kkb;.remove("name")
console.log(kkb.get("name"))

 

posted @ 2020-05-29 11:04  又回到了起点  阅读(190)  评论(0编辑  收藏  举报