The API documentation for Android's SparseIntArray opens with:
SparseIntArrays map integers to integers.
I'm curious, then, why it doesn't implement Map<Integer, Integer>.
It seems to me that all that would've been required is a couple of different method names, a few trivial extra methods, and a bit of code to prohibit null
keys and values... certainly nothing that an EnumMap doesn't handle with grace. Am I overlooking something?
This isn't intended to be a swipe at the designers of the Android API. Normally when I wonder things like this, there turns out to be a good reason, and I learn something about the language or platform.
int is a primitive data type. It only defines that the value is an integer in binary form. It has no inherent methods or fields that you can call. It is not an object and can not be used in an Object reference.
It can store different types: Integer keys and String values or same types: Integer keys and Integer values. HashMap is similar to HashTable, but it is unsynchronized. It is allowed to store null keys as well, but there can only be one null key and there can be any number of null values.
The keys and values of a map can be any reference type. We can't use primitive types because of a restriction around the way generics were designed. A HashMap allows one null key and multiple null values. It doesn't preserve the order of the elements and doesn't guarantee the order will remain the same over time.
The Java platform contains three general-purpose Map implementations: HashMap , TreeMap , and LinkedHashMap . Their behavior and performance are precisely analogous to HashSet , TreeSet , and LinkedHashSet , as described in The Set Interface section.
JavaDoc for SparseIntArray also says
SparseIntArrays map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping.
We can derive following reasons for the choice of SparseIntArray over Map< Integer,Integer > :
As a side point, I would say JavaDoc should be more specific by saying
"SparseIntArrays map primitive integers to integers."
rather than saying
"SparseIntArrays map integers to integers."
Implementing Map
is very heavy. You need methods like entrySet
and keySet
which is not convenient in SparseIntArray
.
Map keys are Objects, so you need constant boxing/unboxing.
SparseIntArray
suggests a different way of enumerating through a Map
, using its specific keyAt
and valueAt
, which are very fast.
If SparseIntArray
implemented Map<Integer, Integer>
you would be tempted to write:
Map<Integer, Integer> intMap = new SparseIntArray();
But then you'd be stuck with only what enumeration capability Map
provides.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With