【发布时间】:2014-05-18 01:30:07
【问题描述】:
我正在编写一个 Dictionary 类的实现。我目前正在写add方法
(public Vadd(K键,V值))
算法:
if the table is too full
rehash
grab the index based on the key
probe with the index and key to resolve collisions
if the table at the index is null, or has been removed
increment the number of entries
increment the number of locations used
set the table at that index to a new tableentry
else
grab the value currently at that index
set the value at the index to the new value
return the old value
我不知道如何根据提供的键获取索引。我也不知道如何引用 Hashtable 中的特定索引。
谢谢
【问题讨论】:
-
每个 Java 对象都有一个
hashCode()方法:docs.oracle.com/javase/7/docs/api/java/lang/…。如果您打算将您的对象用作哈希表中的键,那么您应该覆盖 both 此方法和equals(Object)方法。 -
哈希表的索引取决于哈希函数以及每个键的哈希码。
标签: java algorithm dictionary key hashtable