简体   繁体   English

containsKey HashMap <>的实现-Java

[英]Implementation of containsKey HashMap<> - Java

The whole purpose of using containsKey() is to check whether any given key is already in HashMap or not? 使用containsKey()的整个目的是检查HashMap中是否已存在任何给定的键? If it doesn't contain that key than just add key into that HasMap. 如果不包含该键,则只需将键添加到该HasMap中即可。

But seems like when we call this method it's parameters are Object type that means, containsKey() checks whether given argument(key) has similar memory address with any other already entered key. 但是似乎当我们调用此方法时,它的参数是对象类型,这意味着containsKey()检查给定的parameter(key)是否具有与任何其他已经输入的键相似的内存地址。

Potential Solution: 潜在的解决方案:

One solution could be get a unique data from that object1(oldKey) and check with object2(new key), If they are same than don't use it in HashMap. 一种解决方案是从该object1(oldKey)获取唯一数据,并与object2(new key)进行检查,如果它们相同,则不要在HashMap中使用它。 However this means containsKey has no purpose at all. 但是,这意味着containsKey毫无用处。 Am I right? 我对吗?

Sorry I am not ranting, or probably I sound like one. 抱歉,我并不在意,或者听起来像我。 But I would like to know the most efficient way to get over this problem. 但是我想知道解决这个问题的最有效方法。

will be thankful for any kind of help. 感谢您提供的任何帮助。

But seems like when we call this method it's parameters are Object type that means, containsKey() checks whether given argument(key) has similar memory address with any other already entered key. 但是似乎当我们调用此方法时,它的参数是对象类型,这意味着containsKey()检查给定的parameter(key)是否具有与任何其他已经输入的键相似的内存地址。

Wrong. 错误。 Their equality is checked by comparing their hashCode() values first. 通过首先比较其hashCode()值来检查其相等性。 Only if the hash values are equal, the objects themselves may be compared (but always using equals() , not == ). 仅当哈希值相等时,才可以比较对象本身(但始终使用equals() ,而不是== )。 So any class where these two methods are implemented properly will work correctly as a key in a HashMap . 因此,正确实现这两种方法的任何类都可以作为HashMap的键正常工作。

HashMap.containsKey() methods finds if whether the key's hashCode() exists and not by equality comparison. HashMap.containsKey()方法通过相等性比较来查找键的hashCode()是否存在。 If the hash code exists, it will pull the entry to see if the reference equality OR equals() of the key is equal. 如果哈希码存在,它将拉该条目以查看键的引用相等性或equals()是否相等。

This is implemented in HashMap.getEntry() method: 这是在HashMap.getEntry()方法中实现的:

/**
     * Returns the entry associated with the specified key in the
     * HashMap.  Returns null if the HashMap contains no mapping
     * for the key.
     */
    final Entry<K,V> getEntry(Object key) {
        int hash = (key == null) ? 0 : hash(key.hashCode());
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

But seems like when we call this method it's parameters are Object type 但是似乎当我们调用此方法时,它的参数是对象类型

Yes, but the method will be called on the actual implementation type, not on Object.class. 是的,但是该方法将在实际的实现类型上调用,而不是在Object.class上调用。 That's why it's so important to implement hashCode() properly. 这就是为什么正确实现hashCode()如此重要的原因。

Read: Effective java, Item 9 (in fact you should buy and read the whole book) 阅读: 有效的Java,第9项 (实际上,您应该购买并阅读整本书)

But seems like when we call this method it's parameters are Object type that means, containsKey() checks whether given argument(key) has similar memory address with any other already entered key 但是似乎当我们调用此方法时,它的参数是对象类型,这意味着,containsKey()检查给定的argument(key)是否具有与任何其他已输入键相似的内存地址

This conclusion is wrong. 这个结论是错误的。 The containskey(Object key) calls the equals() method on the passed key , so if this has overriden the equals(Object key) method , then it will resolve correctly based on the key equivalence criteria. containskey(Object key)在传递的键上调用equals()方法,因此,如果此方法已覆盖equals(Object key)方法,则它将根据键等效条件正确解析。 Ofcourse if the Key has not overridden the equals() method , then it is a bad design to start with. 当然,如果Key尚未覆盖equals()方法,那么从一开始就不好设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM