Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't SparseIntArray implement Map<Integer, Integer>?

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.

like image 596
Michael Scheper Avatar asked Feb 28 '14 05:02

Michael Scheper


People also ask

Can we use int in map?

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.

Can we use integer as key in HashMap?

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.

Can we use primitive as key in HashMap?

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.

What type is a Map Java?

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.


2 Answers

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 > :

  • Since we wanted to have mapping between primitive ints, it will be good to avoid autoboxing.
  • In Map, having an object as key/value comes with heaviness of hash calculation, resolving of hash collision, linking of multiple entries in a single bucket etc. These won't be necessary as we are dealing with primitive key/value pairs. Note that SparseIntArray comes with performance warning. Since values are stored in binary search tree array data structure, insertions and deletions will be costly operations. Hence, its a good choice for small set of data.

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."

like image 79
Omkar Shetkar Avatar answered Oct 21 '22 09:10

Omkar Shetkar


  • 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.

like image 29
njzk2 Avatar answered Oct 21 '22 11:10

njzk2