I can think of several reasons why HashMap
s with integer keys are much better than SparseArray
s:
- The Android documentation for a
SparseArray
says “It is generally slower than a traditionalHashMap
“. - If you write code using
HashMap
s rather thanSparseArray
s your code will work with other implementations of Map and you will be able to use all of the Java APIs designed for Maps. - If you write code using
HashMap
s rather thanSparseArray
s your code will work in non-android projects. - Map overrides
equals()
andhashCode()
whereasSparseArray
doesn’t.
Yet whenever I try to use a HashMap
with integer keys in an Android project, IntelliJ tells me I should use a SparseArray
instead. I find this really difficult to understand. Does anyone know any compelling reasons for using SparseArray
s?