当前位置:首页 > JavaScript

js实现哈希

2026-03-15 10:42:33JavaScript

哈希表的基本实现

在JavaScript中,哈希表可以通过对象或Map类实现。对象是键值对的集合,键通常是字符串或Symbol;Map则支持任意类型的键,并提供更丰富的操作方法。

// 使用对象实现
const hashTable = {};
hashTable['key1'] = 'value1';
hashTable['key2'] = 'value2';
console.log(hashTable['key1']); // 输出: value1

// 使用Map实现
const map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
console.log(map.get('key1')); // 输出: value1

自定义哈希函数

若需自定义哈希函数,可将任意数据类型转换为固定长度的哈希值。以下是一个简单字符串哈希函数的实现:

function hashString(str) {
    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        hash = (hash << 5) - hash + str.charCodeAt(i);
        hash |= 0; // 转换为32位整数
    }
    return hash;
}
console.log(hashString('hello')); // 输出: 99162322

处理哈希冲突

哈希冲突可通过开放寻址法或链地址法解决。以下是链地址法的实现示例:

class HashTable {
    constructor(size = 32) {
        this.size = size;
        this.buckets = Array(size).fill(null).map(() => []);
    }

    _hash(key) {
        return hashString(key) % this.size;
    }

    set(key, value) {
        const index = this._hash(key);
        const bucket = this.buckets[index];
        const found = bucket.find(item => item.key === key);
        found ? found.value = value : bucket.push({ key, value });
    }

    get(key) {
        const index = this._hash(key);
        const bucket = this.buckets[index];
        const found = bucket.find(item => item.key === key);
        return found ? found.value : undefined;
    }
}

const table = new HashTable();
table.set('name', 'Alice');
console.log(table.get('name')); // 输出: Alice

实际应用示例

哈希表常用于缓存、频率统计等场景。以下是一个统计字符频率的示例:

function countChars(str) {
    const freq = {};
    for (const char of str) {
        freq[char] = (freq[char] || 0) + 1;
    }
    return freq;
}
console.log(countChars('javascript')); // 输出: { j:1, a:2, v:1, ... }

性能优化

为减少哈希冲突,可动态调整哈希表大小(再哈希)。当元素数量超过负载因子(如70%)时,扩容并重新分配所有键值对:

js实现哈希

class DynamicHashTable extends HashTable {
    constructor() {
        super();
        this.count = 0;
        this.loadFactor = 0.7;
    }

    set(key, value) {
        super.set(key, value);
        this.count++;
        if (this.count / this.size > this.loadFactor) {
            this._resize();
        }
    }

    _resize() {
        const oldBuckets = this.buckets;
        this.size *= 2;
        this.buckets = Array(this.size).fill(null).map(() => []);
        this.count = 0;
        oldBuckets.forEach(bucket => {
            bucket.forEach(({ key, value }) => this.set(key, value));
        });
    }
}

标签: js
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

js实现文件下载

js实现文件下载

使用 a 标签下载文件 通过动态创建 a 标签并设置 download 属性实现文件下载。适用于已知文件 URL 或 Blob 数据的情况。 function downloadFile(url, f…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…

js图片上传实现

js图片上传实现

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API实现基础图片上传功能。HTML部分需要设置accept="image/…

js实现轮播代码

js实现轮播代码

基础轮播实现 使用HTML、CSS和JavaScript创建一个简单的轮播效果。HTML部分定义轮播容器和图片元素。 <div class="carousel"> <div c…