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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…