SparseArray, check if key exists
Hence your value can be null in various situations, I'd suggest to useindexOfKey(int key)
Here is the indexOfKey(int key) reference.
Then just simply check for negative return value
if(mySparseArray.indexOfKey(int) < 0) {
//Item does not exist. Do something relevant
}
You could use:
Bitmap bitmap = cache.get(key, null);
But understand that this is the same as get(key)
:
Bitmap bitmap = cache.get(key);
The best way to use get(key, default)
is to provide a generic default case, something to is a valid substitute when the key is not found.
But there is no good reason not to use if(get(key) != null)
as a quick replacement for contains()
.