App optimization with ArrayMap & SparseArray in Android

Collections are the most common thing used in the software development. In general whenever we required to store data in key value pairs, the very first data structure that comes to our mind is HashMap. It is quite flexible hence it is the most preferred data structure choice to store key value pairs.

To improve the performance of Android application, Android system provides set of collections build especially for mobile development.

So ArrayMap and SparseArray are intended to be more memory efficient than using a HashMap.

How HashMap works?

When a key-value pair is inserted into a HashMap, a hashCode of the key is calculated, and that value is assigned to the hashCode variable of EntryClass. Now using hashCode, we can get the index of the bucket where it will be stored. In case bucket has a pre-existing element, the new element is inserted with the last element pointing to new one essentially making the bucket a linked list.

It has a constant time, or O(1) time requirement for retrieving an element from the array. This means that it takes the same time to pull any element from the array, regardless of the size. This is possible by using a hashing function, which generates the array indices, given the input key.

A Hashmap is used to map Integer keys to an object.

Below are the example code to create HashMap and fetch keys and values:

How ArrayMap works?

When we fetch an item, a binary-search is done on the Hash-Array to find a matching hash the index and then directly return the key-value pair from the second array (Key Value Array). If the key in the second array (Key Value Array), doesn’t match then a linearly walk is done over the second array (Key Value Array) to resolve the collision.

Below are the example code to create ArrayMap and fetch keys and values:

How SparseArray works?

Below are the example code to create SparseArray:

Class appear even in the API 1, but has been redesigned in the API 11. The updated version SparseArrayCompat also available for older devices in the compatibility library.

There are several other types of SparseArray:

LongSparseArray, SparseIntArray, SparseBooleanArray etc.

HashMap can be replaced by the followings Array Class:

Comparison

Benefits to use SparseArray over HashMap is:

· More memory efficient by using primitives

· No auto-boxing

· Allocation-free

Drawbacks:

· For large collections, it is slower

· It only available for Android

In general if inserts or deletes are fairly infrequent, and the number of items is < 1000 then ArrayMap / SparseArray classes are really good replacement classes.

This video from Android developers will provide you further details.

Conclusion

Thanks for reading. To help others please click ❤ to recommend this article if you found it helpful.

Check out my blogger page for more interesting topics on Software development.

You can also follow me at Twitter GitHub

Senior Software Engineer at Mastercard. Dreamer and Achiever..