手动实现简单的HashMap

/**
* @des  自定义简单的HashMap
*/
import java.util.LinkedList;
public class MyMap {

	// 链表数字
	private LinkedList[] arr = null;
	// 大小
	int size;
	//初始化
	public MyMap() {
		this.arr = new LinkedList[10];
	}

	/**
	 * 获取大小的方法
	 * @return
	 */
	public int size() {
		return size;
	}

	/**
	 * map的添加方法
	 * 
	 * @param key
	 * @param value
	 */
	public void put(Object key, Object value) {

		// key 放在 那个具体的链表上
		int index = key.hashCode() % arr.length;

		// 取出链表
		LinkedList list = arr[index];

		// 创建 entry
		Entry entry = new Entry(key, value);

		if (list == null) { // 第一次添加 没有 链表 创建链表
			list = new LinkedList();
			list.add(entry);
			arr[index] = list;

		} else {
			list.add(entry);
		}
		size++;
	}

	/**
	 * 通过key 获取value的值
	 * @param key
	 * @return
	 */
	public Object getValue(Object key) {
		// 1 获取对应的链表
		// key 放在 那个具体的链表上
		int index = key.hashCode() % arr.length;

		// 取出链表
		LinkedList list = arr[index];
		// 2遍历链表 找到entry (key判断 找entry)
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				// 拿到entry
				Entry entry = (Entry) list.get(i);
				if (entry != null) {
					// 判断是我要找的 entry
					if (entry.key.equals(key)) {
						return entry.value;
					}
				}
			}
		}
		return null;
	}
	/**
	 * 是否包含 key
	 * @param key
	 * @return
	 */
	public boolean containsKey(Object key) {
		// 1 获取对应的链表
		// key 放在 那个具体的链表上
		int index = key.hashCode() % arr.length;
		// 取出链表
		LinkedList list = arr[index];
		// 2 便利链表 找到entry (key判断 找entry)
		if (list != null) {

			for (int i = 0; i < list.size(); i++) {
				// 拿到entry
				Entry entry = (Entry) list.get(i);
				if (entry != null) {
					// 判断是我要找的 entry
					if (entry.key.equals(key)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 是否包含 key
	 * @param key
	 * @return
	 */
	public void remove(Object key) {
		// 1 获取对应的链表
		// key 放在 那个具体的链表上
		int index = key.hashCode() % arr.length;

		// 取出链表
		LinkedList list = arr[index];
		// 2 便利链表 找到entry (key判断 找entry)

		if (list != null) {

			for (int i = 0; i < list.size(); i++) {
				// 拿到entry
				Entry entry = (Entry) list.get(i);
				if (entry != null) {
					// 判断是我要找的 entry
					if (entry.key.equals(key)) {
						list.remove(entry);
						break;
					}
				}
			}
		}
		size--;
	}

	class Entry {

		Object key;
		Object value;

		public Entry(Object key, Object value) {
			this.key = key;
			this.value = value;
		}
	}

}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中可以使用STL中的unordered_map来实现hashmap,也可以手动实现hashmap。 以下是一个简单手动实现hashmap的示例代码: ```c++ #include <iostream> #include <vector> #include <list> using namespace std; class HashMap { private: vector<list<pair<int, int>>> table; // 存储桶 int capacity; // hashmap容量 int size; // hashmap当前大小 // 获取哈希码 int hash(int key) { return key % capacity; } public: HashMap(int capacity) { this->capacity = capacity; this->size = 0; table.resize(capacity); } // 插入键值对 void put(int key, int value) { int index = hash(key); auto &bucket = table[index]; // 获取对应桶 for (auto &node : bucket) { // 查找是否已存在key if (node.first == key) { node.second = value; // 更新value return; } } bucket.emplace_back(key, value); // 插入新节点 size++; // 更新大小 } // 获取key对应的value int get(int key) { int index = hash(key); auto &bucket = table[index]; // 获取对应桶 for (auto &node : bucket) { // 查找是否存在key if (node.first == key) { return node.second; // 返回对应value } } return -1; // 未找到对应key } // 删除key对应的节点 void remove(int key) { int index = hash(key); auto &bucket = table[index]; // 获取对应桶 for (auto iter = bucket.begin(); iter != bucket.end(); iter++) { // 查找是否存在key if (iter->first == key) { bucket.erase(iter); // 移除节点 size--; // 更新大小 return; } } } // 获取hashmap当前大小 int getSize() { return size; } }; int main() { HashMap hashMap(10); hashMap.put(1, 10); hashMap.put(2, 20); hashMap.put(11, 110); hashMap.put(21, 210); cout << hashMap.get(1) << endl; // 10 cout << hashMap.get(11) << endl; // 110 cout << hashMap.get(3) << endl; // -1 hashMap.remove(2); cout << hashMap.getSize() << endl; // 3 return 0; } ``` 在该示例代码中,使用vector来存储桶,每个桶是一个链表,存储键值对。在插入、获取、删除操作时,先根据key的哈希值计算出对应的桶,再在桶中查找是否存在对应的键值对。如果存在,则进行更新或移除操作;如果不存在,则进行插入操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值