当前位置:首页 > 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
分享给朋友:

相关文章

js实现验证码

js实现验证码

实现验证码的JavaScript方法 生成随机验证码 使用Math.random()生成随机字符串,结合数字和字母: function generateCaptcha() { const cha…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…