[英]If key in hashtable is a class object, how does containsKey work?
When we put a class Object (that has say three data members) in a hashtable, how can I prevent putting another entry into the hash table whose key has the same three data members ? 当我们在哈希表中放置一个类Object(有三个数据成员)时,如何防止将另一个条目放入其密钥具有相同三个数据成员的哈希表中? Cos I am guessing this will be a new object.
我猜这将是一个新的对象。 So hashtable.containsKey() will return false even when there is a key (this class object) that has the very same data members as the one that is waiting to be inserted.
因此,即使有一个键(此类对象)具有与等待插入的数据成员完全相同的数据成员,hashtable.containsKey()也将返回false。
More clearly: I have a class like 更清楚:我有一个类似的课程
class Triplet {
private Curr curr;
private Prev prev;
private Next next;
}
I have a hashtable structure like: 我有一个哈希表结构,如:
Hashtable<Triplet, Integer> table = new Hashtable<Triplet, Integer>();
When I do: 当我做:
if(!table.containsKey(triplet_to_inserted))
table.put(triplet, new Integer(0));
will this insert a duplicate even if the table contains a triplet that already has the same data members ? 即使表中包含已具有相同数据成员的三元组,这是否会插入副本? That is: triplet_to_be_inserted.curr, triplet_to_be_inserted.next and triplet_to_be_inserted.prev If yes, how to prevent this ?
即:triplet_to_be_inserted.curr,triplet_to_be_inserted.next和triplet_to_be_inserted.prev如果是,如何防止这种情况?
Also, for any entry to be inserted, will containsKey() ever return true at all ? 此外,对于要插入的任何条目,containsKey()是否会返回true? How to work around this problem ?
如何解决这个问题?
Thanks. 谢谢。
All classes that have instances used as keys in a hash-like data structure must correctly implement the equals
and hashCode
methods. 所有具有在类哈希数据结构中用作键的实例的类必须正确实现
equals
和hashCode
方法。 Brian Goetz has a great article on this from a while back. 不久前,Brian Goetz就此发表了一篇很棒的文章 。
Without knowing the structure of Curr
, Prev
and Next
and exact example is difficult, but assuming they are not null and have sensible hashCode
implementations, you could do something like this: 在不知道
Curr
, Prev
和Next
的结构的情况Next
,确切的示例很难,但假设它们不是null并且具有合理的hashCode
实现,那么您可以执行以下操作:
public boolean equals(Object obj) {
if (!(obj instanceof Triplet)) {
return false;
} else {
Triplet that = (Triplet)obj;
return this.curr.equals(that.curr) &&
this.next.equals(that.next) &&
this.prev.equals(that.prev);
}
}
public int hashCode() {
int hash = this.curr.hashCode();
hash = hash * 31 + this.next.hashCode();
hash = hash * 31 + this.prev.hashCode();
return hash;
}
The easiest way is to use Eclipse's generate hashCode() and equals(). 最简单的方法是使用Eclipse的生成hashCode()和equals()。 You can select which members should be taken into account for the hashCode and equals calculation, so in case you have some transient members (you don't) you can only use those which are relevant.
您可以选择hashCode和equals计算应考虑哪些成员,因此如果您有一些临时成员(您没有),您只能使用相关的成员。
And similiarly (and recursively) for Curr, Prev and Next... 和Curr,Prev和Next类似(并且递归地)...
From java documentation: 来自java文档:
To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
要成功存储和检索哈希表中的对象,用作键的对象必须实现hashCode方法和equals方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.