Domanda

Developing a program which uses hash map with integer as keys and objects as values. I keep on getting Lint warning informing SparseArray is more efficient and when I read about the same it was given in this Link that, there can be gaps in the indices. What does that actually mean, I am not able to get.

Does it mean, if I have keys like 1,5, 10 then array size will be 3 with indices 1, 5, 10. (Or) array size will be 11 with objects present for 1, 5, 10 indices and rest are empty?

Please clarify me.

È stato utile?

Soluzione 2

It makes sense more if you look at the source code for SparseArray. All it has is two arrays - one containing the keys and the other containing the values; so your first reasoning is correct.

Altri suggerimenti

The increased efficiency of SparseArray is not only due to the different structure (as the other answers pointed out), but also due to the fact that you are avoiding autoboxing when using int as a key.

In other words, when using e.g. map.get(1), you are actually performing the equivalent of map.get(Integer.valueOf(1)). That is, you're converting an int primitive to an Integer class instance.

With SparseArray, there's no type conversion of this kind, you're just using an int. Check out the autoboxing link for more details.

You can have the keys 1, 5 & 10 from your example, effectively resulting in a SparseArray with the length of 3.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top