JAVA手写实现简单的哈希表

JAVA手写实现简单的哈希表

说明:map在日常的开发和学习中非常重要,因为他的插入效率和查找效率以及删除效率都接近于O(1),拿hashtable来说,底层是依赖数组,数组根据下表查找删除插入都是O(1)。

实现分析

哈希表重要一个步骤就是实现哈希函数,根据哈希函数生成对应哈希值,根据哈希值往数组中插入或者删除,查询对应的值。所以关键就在于哈希函数的实现,以及解决哈希冲突。

实现背景

一个班级,同学们有学号,学号的类型为1611001,1611002,16110xx,根据这个特点我只需要截取后两位不同的哈希函数,但是限制100人一类,下一次,将会更新完整的哈希表,主要实现哈希的核心。

哈希函数的编写

由于学号的后两位不同,所以我们可以截取后两位的值,转化整对应int值。

public int hash(String id) {
		String lastTowChars=id.substring(id.length()-2,id.length());
		int hashValue=Integer.parseInt(lastTowChars);
		return hashValue;
	}

实现代码的完成过程

public class MyHashTable {
	private String[] str=new String[5];
	
	public int hash(String id) {
		String lastTowChars=id.substring(id.length()-2,id.length());
		int hashValue=Integer.parseInt(lastTowChars);
		return hashValue;
	}
	public boolean insertValue(String key,String value) {
		try {
			int strMark=hash(key);
			str[strMark]=value;
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}
	public String searchValueByKey(String key) {
		int hash = hash(key);
		return str[hash];
	}
}

在Main方法中测试

public class myMain {
	public static void main(String[] args) {
		MyHashTable mht=new MyHashTable();
		mht.insertValue("0001", "superpopo");
		mht.insertValue("0002", "hahaha");
		mht.insertValue("0003", "gagaga");
		System.out.println(mht.searchValueByKey("0001"));
		System.out.println(mht.searchValueByKey("0002"));
		System.out.println(mht.searchValueByKey("0003"));
	}
}

输出的结果

### 回答1: 可以通过使用数组来手写 HashMap 的实现。每个数组项可以包含一个键值对。当想要添加一个键值对时,可以使用哈希函数将键转换为数组索引,并将值存储在该索引处。当想要检索与特定键关联的值时,可以使用哈希函数计算键的数组索引,并检索该索引处的值。 下面是一个示例实现: ``` class HashMap { private static class Entry { private final Object key; private Object value; private Entry next; public Entry(Object key, Object value, Entry next) { this.key = key; this.value = value; this.next = next; } } private Entry[] table; public HashMap(int capacity) { table = new Entry[capacity]; } public void put(Object key, Object value) { int index = key.hashCode() % table.length; Entry entry = new Entry(key, value, null); if (table[index] == null) { table[index] = entry; } else { Entry current = table[index]; while (current.next != null) { if (current.key.equals(key)) { current.value = value; return; } current = current.next; } current.next = entry; } } public Object get(Object key) { int index = key.hashCode() % table.length; Entry current = table[index]; while (current != null) { if (current.key.equals(key)) { return current.value; } current = current.next; } return null; } } ``` ### 回答2: 要手写 HashMap 的实现,首先需要了解 HashMap 的原理。HashMap 是一种基于哈希表的数据结构,用于存储键值对。它通过使用键的哈希码来计算存储位置,从而能够快速地在 O(1) 的时间复杂度下查找、插入和删除键值对。 下面是实现 HashMap 的基本步骤: 1. 创建一个存储键值对的数组,称为桶(bucket)。桶的长度可以根据实际需求进行调整。 2. 实现一个方法来计算键的哈希码。这个方法应该根据键的特性,如字符串的字符值之和、对象的唯一标识等,将键转化为一个整数。 3. 使用哈希码计算键的存储位置。一般情况下,可以通过将哈希码与桶的长度取模来得到存储位置。 4. 如果桶的该位置为空,则直接将键值对存储在该位置。 5. 如果桶的该位置已经被占用,并且键与已经存储的键相等,则更新该键对应的值。 6. 如果桶的该位置已经被占用,并且键与已经存储的键不相等,则发生了哈希冲突。可以采用链表或者其他解决冲突的方式来解决。可以在每个桶中使用一个链表,当发生冲突时,将新的键值对链接到链表的末尾。 7. 实现一个方法来获取键对应的值。这个方法应该根据键的哈希码,找到存储位置,并遍历可能存在的链表来找到键对应的值。 8. 实现一个方法来删除键值对。这个方法应该根据键的哈希码,找到存储位置,并遍历可能存在的链表来删除键值对。 9. 可以考虑实现一些其他方法,如获取键的集合、获取值的集合、获取键值对的集合等。 总的来说,手写 HashMap 的实现涉及到计算哈希码、解决哈希冲突、桶的动态调整以及实现各种操作的方法。这需要对哈希表的原理有一定的理解,并且熟悉相关的数据结构和算法。实际实现过程中还需要考虑到性能优化、并发访问的安全等方面的问题。 ### 回答3: 要手写实现一个简单的HashMap,需要理解HashMap的基本原理和数据结构。以下是一个简单实现示例: 首先,需要定义一个HashMap的主要数据结构,这个数据结构用于存储键值对。可以使用一个数组来实现桶(bucket),每个桶中存储一个链表,用于处理哈希冲突。 ```java public class MyHashMap<K, V> { private static final int SIZE = 16; // 默认的桶大小 private Node<K, V>[] buckets; // 存储桶的数组 private static class Node<K, V> { private final K key; private V value; private Node<K, V> next; public Node(K key, V value) { this.key = key; this.value = value; } // 省略 getter 和 setter 方法 } public MyHashMap() { this.buckets = new Node[SIZE]; } // 根据key计算hash值 private int getHash(K key) { return key.hashCode() % SIZE; } // 向HashMap中放入新的键值对 public void put(K key, V value) { int index = getHash(key); Node<K, V> newNode = new Node<>(key, value); if (buckets[index] == null) { buckets[index] = newNode; } else { Node<K, V> currentNode = buckets[index]; while (currentNode.next != null) { if (currentNode.key.equals(key)) { currentNode.value = value; // 如果已经存在该key,则更新其value return; } currentNode = currentNode.next; } currentNode.next = newNode; } } // 根据key获取HashMap中的值 public V get(K key) { int index = getHash(key); Node<K, V> currentNode = buckets[index]; while (currentNode != null) { if (currentNode.key.equals(key)) { return currentNode.value; } currentNode = currentNode.next; } return null; } } ``` 上述示例中,我们利用一个大小为16的数组作为HashMap的桶,每个桶是一个链表,用于解决哈希冲突。put()方法根据计算得到的哈希值找到对应的桶,然后遍历链表,如果已经存在相同的key,则更新value,否则在链表末尾添加一个新节点。get()方法也是通过哈希值找到对应的桶,然后在链表中查找相应的key,返回对应的value。 这只是一个简单的HashMap实现,没有考虑一些复杂的情况和性能优化,真实的HashMap要比这个实现复杂得多。但是这个实现提供了一个基本的思路和框架,可以用来理解HashMap的基本原理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值