Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using java Map.containsKey() redundant when using map.get()

I have been wondering for some time whether it is allowable within best practice to refrain from using the containsKey() method on java.util.Map and instead do a null check on the result from get().

My rationale is that it seems redundant to do the lookup of the value twice - first for the containsKey() and then again for get().

On the other hand it may be that most standard implementations of Map cache the last lookup or that the compiler can otherwise do away with the redundancy, and that for readability of the code it is preferable to maintain the containsKey() part.

I would much appreciate your comments.

like image 328
Erik Madsen Avatar asked Jan 30 '13 09:01

Erik Madsen


People also ask

What is the significant difference between containsKey () and get ()?

get(Object) does explicitly warn about the subtle differences between Map. get(Object) and Map. containsKey(Object) : 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 .

What is map containsKey in Java?

The java. util. Map. containsKey() method is used to check whether a particular key is being mapped into the Map or not. It takes the key element as a parameter and returns True if that element is mapped in the map.

What will be the time complexity to perform containsKey () operation in maps?

Time Complexity: containsKey() is O(1) in Average Case, and O(n) in worst case.

Is HashMap containsKey constant time?

From the API doc of HashMap: This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.


2 Answers

Some Map implementations are allowed to have null values, eg HashMap, in this case if get(key) returns null it does not guarantee that there is no entry in the map associated with this key.

So if you want to know if a map contains a key use Map.containsKey. If you simply need a value mapped to a key use Map.get(key). 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; In such case Map.containsKey is useless and will affect performance. Moreover, in case of concurrent access to a map (eg ConcurrentHashMap), after you tested Map.containsKey(key) there is a chance that the entry will be removed by another thread before you call Map.get(key).

like image 194
Evgeniy Dorofeev Avatar answered Sep 24 '22 22:09

Evgeniy Dorofeev


I think it is fairly standard to write:

Object value = map.get(key); if (value != null) {     //do something with value } 

instead of

if (map.containsKey(key)) {     Object value = map.get(key);     //do something with value } 

It is not less readable and slightly more efficient so I don't see any reasons not to do it. Obviously if your map can contain null, the two options don't have the same semantics.

like image 27
assylias Avatar answered Sep 24 '22 22:09

assylias