开发者

Implementation of containsKey HashMap<> - Java

开发者 https://www.devze.com 2023-04-04 15:51 出处:网络
The whole purpose of using containsKey() is to check whether any given key is already in HashMap or not? If it doesn\'t contain that key than just add key into that HasMap.

The whole purpose of using containsKey() is to check whether any given key is already in HashMap or not? If it doesn't contain that key than just add key into that 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.

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. However this means containsKey has no purpose at all. 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.

Wrong. Their equality is checked by comparing their hashCode() values first. Only if the hash values are equal, the objects themselves may be compared (but always using equals(), not ==). So any class where these two methods are implemented properly will work correctly as a key in a HashMap.


HashMap.containsKey() methods finds if whether the key's hashCode() exists and not by equality comparison. If the hash code exists, it will pull the entry to see if the reference equality OR equals() of the key is equal.

This is implemented in HashMap.getEntry() method:

/**
     * 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. That's why it's so important to implement hashCode() properly.

Read: Effective java, Item 9 (in fact you should buy and read the whole book)


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

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. Ofcourse if the Key has not overridden the equals() method , then it is a bad design to start with.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号