1、HashCode实现

  1. // 散列函数实现, 长度11的素数为例
  2. const handleHashCode = (key = "") => {
  3. let hashCode = 0;
  4. for(let i=0; i<key.length; i++) {
  5. // 注: charCodeAt() 方法可返回指定位置的字符的 Unicode 编码
  6. hashCode += key.charCodeAt(i)
  7. }
  8. // console.log("hashCode: ", hashCode % 11)
  9. // 素数: 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061
  10. return hashCode % 11
  11. }

2、开放定址法实现hashMap

  1. const { handleHashCode } = require('./hashCode')
  2. // put进去的object = {key: value}
  3. class putValue {
  4. constructor(key, value) {
  5. this.key = key
  6. this.value = value
  7. }
  8. }
  9. // 实现hashMapLine
  10. class HashMapLine {
  11. constructor() {
  12. this.map = []
  13. }
  14. // put元素
  15. put(key, value) {
  16. // 1、获取位置
  17. let position = handleHashCode(key)
  18. // 2、position位置上的值不为undefined、null
  19. while(this.map[position] !== undefined && this.map[position] !== null) {
  20. position += 1
  21. }
  22. // 3、插入值
  23. this.map[position] = new putValue(key, value)
  24. }
  25. // get元素
  26. get(key) {
  27. let position = handleHashCode(key)
  28. // 1、判断position位置的元素是否存在
  29. while(this.map[position] !== undefined) {
  30. // 2、判断position位置的key相等就返回value, 否则+1
  31. if(this.map[position] !== null && this.map[position].key === key) {
  32. return this.map[position].value
  33. }
  34. position += 1
  35. }
  36. return false
  37. }
  38. // remove元素
  39. remove(key) {
  40. let position = handleHashCode(key)
  41. // 1、判断position位置的元素是否存在
  42. while(this.map[position] !== undefined) {
  43. // 2、判断position位置的key相等就将postion置位null, 否则+1
  44. if(this.map[position] !== null && this.map[position].key === key) {
  45. this.map[position] = null
  46. return true
  47. }
  48. position += 1
  49. }
  50. return false
  51. }
  52. getItems() {
  53. return this.map
  54. }
  55. }
  56. // 测试 case
  57. const hashmap = new HashMapLine()
  58. hashmap.put("Donnie", "1111@qq.com")
  59. hashmap.put("ab", "2222@qq.com")
  60. hashmap.put("Ana", "3333@qq.com")
  61. console.log("hashCode: ", handleHashCode("ab"))
  62. console.log("hashCode: ", handleHashCode("Ana"))
  63. console.log(hashmap.getItems())
  64. hashmap.remove("Donnie")
  65. hashmap.remove("Ana")
  66. console.log(hashmap.getItems())
  67. hashmap.put("Ana", "4444@qq.com")
  68. console.log(hashmap.getItems())

3、拉链法实现hashMap

  1. const { handleHashCode } = require('./hashCode')
  2. const { SingleLinkedList } = require('./LinkedList')
  3. // put进去的object = {key: value}
  4. class putValue {
  5. constructor(key, value) {
  6. this.key = key
  7. this.value = value
  8. }
  9. }
  10. class HashMapLinked {
  11. constructor() {
  12. this.map = []
  13. }
  14. // put<K, V>
  15. put(key, value) {
  16. let position = handleHashCode(key)
  17. if(this.map[position] === undefined) {
  18. // 第一次插入, 初始化链表
  19. this.map[position] = new SingleLinkedList()
  20. }
  21. this.map[position].append(new putValue(key, value))
  22. }
  23. // get<K>
  24. get(key) {
  25. let position = handleHashCode(key)
  26. // 判断当前位置是否存在元素
  27. if(this.map[position] === undefined) {
  28. return false
  29. }else {
  30. let currentNode = this.map[position].head
  31. // 找到node节点
  32. while(currentNode) {
  33. if(currentNode.val.key === key) {
  34. return currentNode.val.value
  35. }
  36. currentNode = currentNode.next
  37. }
  38. return false
  39. }
  40. }
  41. // remove<K>
  42. remove(key) {
  43. let position = handleHashCode(key)
  44. if(this.map[position] === undefined) {
  45. return false
  46. }else {
  47. let currentNode = this.map[position].head
  48. let index = 0
  49. while(currentNode) {
  50. // 删除链表对应的位置
  51. if(currentNode.val.key === key) {
  52. // console.log(this.map[position])
  53. this.map[position].remove(index)
  54. }
  55. // 空链表进行初始化
  56. if(this.map[position].isEmpty()) {
  57. this.map[position] = undefined
  58. }
  59. currentNode = currentNode.next
  60. index += 1
  61. }
  62. return false
  63. }
  64. }
  65. getItems() {
  66. return this.map
  67. }
  68. }
  69. // 测试 case
  70. const hashmap = new HashMapLinked()
  71. hashmap.put("Donnie", "1111@qq.com")
  72. hashmap.put("ab", "2222@qq.com")
  73. hashmap.put("Ana", "3333@qq.com")
  74. console.log("hashCode: ", handleHashCode("ab"))
  75. console.log("hashCode: ", handleHashCode("Ana"))
  76. console.log(hashmap.getItems())
  77. hashmap.remove("Donnie")
  78. hashmap.remove("Ana")
  79. console.log(hashmap.getItems())
  80. hashmap.put("Ana", "4444@qq.com")
  81. console.log(hashmap.getItems())