Map lookup performance

To answer your question,
"How does the performance of given cases change with respect to the Map implementation?"
The performance difference is negligible.

To comment on your comment,
"In the second snippet I don't like the fact that value is declared with a wider scope."
Good, you shouldn't. You see, there are two ways to get null returned from a Map:

  1. The key doesn't exist OR
  2. The key does exist, but its value is null (if the Map implementation allows null values, like HashMap).

So the two scenarios could actually have different results if the key existed with a null value!

EDIT

I wrote the following code to test out the performance of the two scenarios:

public class TestMapPerformance {

    static Map<String, String> myMap = new HashMap<String, String>();
    static int iterations = 7000000;

    // populate a map with seven million strings for keys
    static {
        for (int i = 0; i <= iterations; i++) {
            String tryIt = Integer.toString(i);
            myMap.put(tryIt, "hi");
        }
    }
    // run each scenario twice and print out the results.
    public static void main(String[] args) {
        System.out.println("Key Exists: " + testMap_CheckIfKeyExists(iterations));
        System.out.println("Value Null: " + testMap_CheckIfValueIsNull(iterations));
        System.out.println("Key Exists: " + testMap_CheckIfKeyExists(iterations));
        System.out.println("Value Null: " + testMap_CheckIfValueIsNull(iterations));
    }

    // Check if the key exists, then get its value  
    public static long testMap_CheckIfKeyExists(int iterations) {       
        Date date = new Date();
        for (int i = 0; i <= iterations; i++) {
            String key = Integer.toString(i);
            if(myMap.containsKey(key)) {
                String value = myMap.get(key);
                String newString = new String(value);
            }
        }
        return new Date().getTime() - date.getTime();
    }

    // Get the key's value, then check if that value is null
    public static long testMap_CheckIfValueIsNull(int iterations) {
        Date date = new Date();
        for (int i = 0; i <= iterations; i++) {
            String key = Integer.toString(i);
            String value = myMap.get(key);
            if(value != null) {
                String newString = new String(value);
            }
        }
        return new Date().getTime() - date.getTime();
    }

}

I ran it and this was the result:

Key Exists: 9901
Value Null: 11472
Key Exists: 11578
Value Null: 9387

So in conclusion, the difference in performance in negligible.


Map is an interface, so the implementing classes have quite a bit of freedom in how they implement each operation (it's entirely possible to write a class that buffers the last entry, which may allow constant time access for the get operation if it's the same as the last gotten object, making the two practically equivalent, except for a presumably required comparison).

For TreeMap and HashMap, for example, containsKey is essentially just a get operation (more specifically getEntry) with a check for null.

Thus, for these two containers, the first version should take roughly twice as long as the second (assuming you use the same type of Map in both cases).

Note that HashMap.get is O(1) (with a hash function well-suited to the data) and TreeMap.get is O(log n). So if you do any significant amount of work in the loop, and the Map doesn't contain in the order of millions of elements, the difference in performance is likely to be negligible.

However, note the disclaimer in the docs for Map.get:

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.