vishal moradiya vishal moradiya - 1 month ago 4
Android Question

Difference between SparseArray and Hashmap?

i want to know performance and efficency between

SparseArray
and
HashMap
and which one is better to use. i can't undestand when to use
SparseArray
and when
Hashmap

Answer

SparseArray is the choice you should make when your map key is an integer, and those integers are not sequential, meaning not 0, 1, 2, 3, 4, ... n - but more like, 43, 2045, 12, 5.

Now, let's assume for a moment that your keys were integers, but also sequential (0, 1, 2, 3, 4, ... n). What would be the best choice of collection in this case? Well, lets assume that your values are Strings. In this case, your best choice here would be a String array (String[]). The reason for this is that the String array answers all your needs with a minimum impact on performance.

myStringArray[0] would return the String value of the key '0', and so on.

SparseArray works exactly like this, except we allow some values inside the array to be empty (hence, 'sparse'). A SparseArray will create a large array behind the scenes, and use the integer keys as the indexes of the array where the values are placed.

Essentially, HashMaps also work this way - behind the scenes there is a large integer array which is used to place values. The only difference is that in HashMaps, the keys are not integers, they could be anything. For sake of our example, lets say the keys are Strings. What a HashMap will do is it will use a hash function to convert this String key into an integer, then use that to position the value in the array. This can lead to cases where two different Strings produce the same integers. In these cases, the HashMap will perform another operation such as queueing values in the array, or producing a new key, and so on. This, and the production of an integer key via a hash function, can sometimes involve quite a bit of performance.

SparseArray allows you to skip this entirely since with integer keys, none of this is necessary, and therefore is more efficient.