Design a HashMap class in C++. Implement put, get methods.

本文介绍了一个简单的HashMap数据结构实现,使用C++编程语言。该实现包括插入和查找操作,并使用链地址法解决冲突。

要实现的HashMap结构如图:
这里写图片描述

Code:

// Compiled with: g++ -Wall -std=c++14 -pthread

#include <iostream>
#include <string>

using namespace std;

class HashMap {
    typedef struct entry {
        unsigned int key;
        string value;
        entry* next;
        unsigned int hash;
    } entry;

    entry* Bucket[16] = {nullptr};

  public:
    void Put(const unsigned int key, const string& value) {
        unsigned int ind = key%16;
        entry* i;
        for (i = Bucket[ind]; i != nullptr; i = i->next) {
            if (i->key == key) {
                i->value = value;
                break;
            }
        }

        if (i == nullptr) {
            entry *node = new entry(); 
            node->key = key;
            node->value = value;
            node->next = Bucket[ind];
            node->hash = ind;
            Bucket[ind] = node;    
        }
    }

    entry* Get(const unsigned key) {
        unsigned int ind = key%16;
        entry* i;
        for (i = Bucket[ind]; i != nullptr; i = i->next) {
            if (i->key == key) {
                break;
            }
        }
        return i;
    }
};

int main(){
    HashMap hashmap;

    hashmap.Put(0, "hello");
    hashmap.Put(1, "HELLO");
    hashmap.Put(18, "hEllo18");
    hashmap.Put(16, "hello16");
    hashmap.Put(17, "world");

    unsigned int testkey = 0;
    if (!hashmap.Get(testkey)) {
        cout << "no found" << endl;
    }
    else {
        cout << hashmap.Get(testkey)->value << endl;
    }

    cout << "Hello, World!" << endl;
    return 0;
}
在 Java 中使用 `HashMap.get()` 方法时出现 `class java.util.HashMap cannot be cast to class java.lang.String` 异常,是因为代码尝试将 `HashMap` 对象强制转换为 `String` 类型,而这两种类型不兼容。解决办法有以下几种: #### 检查键对应的值类型 在使用 `get()` 方法获取值后,先检查值的类型是否为 `String`,再进行相应的操作。示例代码如下: ```java import java.util.HashMap; import java.util.Map; public class CheckValueTypeExample { public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("key1", new HashMap<>()); map.put("key2", "value2"); Object value = map.get("key1"); if (value instanceof String) { String strValue = (String) value; System.out.println(strValue); } else { System.out.println("Value is not a String."); } } } ``` #### 确保存储正确类型的值 在向 `HashMap` 中存储值时,确保只存储 `String` 类型的值。示例代码如下: ```java import java.util.HashMap; import java.util.Map; public class StoreCorrectTypeExample { public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); String value = map.get("key1"); System.out.println(value); } } ``` #### 处理不同类型的值 如果 `HashMap` 中可能存储不同类型的值,可以根据不同类型进行不同的处理。示例代码如下: ```java import java.util.HashMap; import java.util.Map; public class HandleDifferentTypesExample { public static void main(String[] args) { Map<String, Object> map = new HashMap<>(); map.put("key1", new HashMap<>()); map.put("key2", "value2"); Object value = map.get("key1"); if (value instanceof String) { String strValue = (String) value; System.out.println(strValue); } else if (value instanceof HashMap) { HashMap<?, ?> hashMapValue = (HashMap<?, ?>) value; System.out.println("Value is a HashMap: " + hashMapValue); } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值