Key existence check in HashMap

后端 未结 10 1852
走了就别回头了
走了就别回头了 2020-11-27 09:03

Is checking for key existence in HashMap always necessary?

I have a HashMap with say a 1000 entries and I am looking at improving the efficiency. If the HashMap is b

相关标签:
10条回答
  • 2020-11-27 09:43

    Just use containsKey() for clarity. It's fast and keeps the code clean and readable. The whole point of HashMaps is that the key lookup is fast, just make sure the hashCode() and equals() are properly implemented.

    0 讨论(0)
  • 2020-11-27 09:44

    The Jon Skeet answer addresses well the two scenarios (map with null value and not null value) in an efficient way.

    About the number entries and the efficiency concern, I would like add something.

    I have a HashMap with say a 1.000 entries and I am looking at improving the efficiency. If the HashMap is being accessed very frequently, then checking for the key existence at every access will lead to a large overhead.

    A map with 1.000 entries is not a huge map.
    As well as a map with 5.000 or 10.000 entries.
    Map are designed to make fast retrieval with such dimensions.

    Now, it assumes that hashCode() of the map keys provides a good distribution.

    If you may use an Integer as key type, do it.
    Its hashCode() method is very efficient since the collisions are not possible for unique int values :

    public final class Integer extends Number implements Comparable<Integer> {
        ...
        @Override
        public int hashCode() {
            return Integer.hashCode(value);
        }
    
        public static int hashCode(int value) {
            return value;
        }
        ...
    }
    

    If for the key, you have to use another built-in type as String for example that is often used in Map, you may have some collisions but from 1 thousand to some thousands of objects in the Map, you should have very few of it as the String.hashCode() method provides a good distribution.

    If you use a custom type, override hashCode() and equals() correctly and ensure overall that hashCode() provides a fair distribution.
    You may refer to the item 9 of Java Effective refers it.
    Here's a post that details the way.

    0 讨论(0)
  • 2020-11-27 09:45

    I usually use the idiom

    Object value = map.get(key);
    if (value == null) {
        value = createValue(key);
        map.put(key, value);
    }
    

    This means you only hit the map twice if the key is missing

    0 讨论(0)
  • 2020-11-27 09:48

    Do you ever store a null value? If not, you can just do:

    Foo value = map.get(key);
    if (value != null) {
        ...
    } else {
        // No such key
    }
    

    Otherwise, you could just check for existence if you get a null value returned:

    Foo value = map.get(key);
    if (value != null) {
        ...
    } else {
        // Key might be present...
        if (map.containsKey(key)) {
           // Okay, there's a key but the value is null
        } else {
           // Definitely no such key
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:48
    if(map.get(key) != null || (map.get(key) == null && map.containsKey(key)))
    
    0 讨论(0)
  • 2020-11-27 09:49

    Do you mean that you've got code like

    if(map.containsKey(key)) doSomethingWith(map.get(key))

    all over the place ? Then you should simply check whether map.get(key) returned null and that's it. By the way, HashMap doesn't throw exceptions for missing keys, it returns null instead. The only case where containsKey is needed is when you're storing null values, to distinguish between a null value and a missing value, but this is usually considered bad practice.

    0 讨论(0)
提交回复
热议问题