简体   繁体   English

如果哈希表中的键是一个类对象,那么containsKey是如何工作的?

[英]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. 所有具有在类哈希数据结构中用作键的实例的类必须正确实现equalshashCode方法。 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: 在不知道CurrPrevNext的结构的情况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.

相关问题 HashMap的ContainsKey不适用于Custom Class作为键 - ContainsKey of HashMap does not work with a Custom Class as key 如何使Java Hashtable.containsKey适用于Array? - How do I make Java Hashtable.containsKey to work for Array? 即使字符串和哈希码不同,Java Hashtable .containsKey(String key)也会返回true。 - Java Hashtable .containsKey(String key) is returning true even when the strings and hashcodes are different… How? java.util.HashMap.containsKey(Object key) 实现是否违反 java.util.Map.containsKey(Object key) 文档? - Does java.util.HashMap.containsKey(Object key) implementation violate java.util.Map.containsKey(Object key) documentation? 即使在哈希表变大之后,get(key) 是如何工作的! - How does the get(key) work even after the hashtable has grown in size! 有没有办法在对象键上使用 containsKey() 映射函数? - Is there a way to use the containsKey() map function on an Object key? 如何将哈希表转换为抽象类对象 - How to Downcast the hashtable into an abstract class object 哈希表getcontainsKey不起作用 - HashTable getcontainsKey does not work 如果哈希表<e, ?> .containsKey(Object o) 为某个 object 返回 true,我可以安全地将 Object 转换为 E 吗?</e,> - If Hashtable<E, ?>.containsKey(Object o) returns true for a certain object, am I safe to cast that Object to E? 将类作为Hashtable密钥-这是一个好主意吗? - Class as Hashtable key — is it a good idea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM