Android ArrayMap and SparseArray: Working Comparison and Conclusion

4 minutes read

ArrayMap and SparseArray

Collection refers to a task when software development is concerned. HashMap is first data structure to emerge in mind when we look to store data in key-value pairs. On behalf of its flexibility, it is the most popular data structure choice. In order to boost the performance of Android App, there are two important set of collection ArrayMap and SparseArray are built exclusively for mobile. Thereby, substantially more memory efficient in comparison to a HashMap collection.


ArrayMap Working:
There are 2 smaller arrays inside an ArrayMap. Hash-Array (first array) holds specific hash keys in a sorted manner. The keys and values relating to the objects as per the first array are contained in the second array. The second array is also known as Key Value Array. A binary search is done on the Hash-Array upon fetching an item to find a matching hash the index after which key-value pair from the second array is returned directly. In case, when the key is inside the second array doesn’t match then the second array is used to avoid the collision.


Image Credits:


Below code is used for the creation of ArrayMap and fetch keys and values:

ArrayMap<String, String> arrayMap = new ArrayMap<>();
arrayMap.put(“Key1”, “Value1”);
arrayMap.put(“Key2”, “Value2”);
arrayMap.put(“Key3”, “Value3”); 

for (int i = 0; i < arrayMap.size(); i++) {
  String key = arrayMap.keyAt(i);
  String value = arrayMap.valueAt(i);


SparseArray Working:   
The major difference between ArrayMap and SpareArray is that latter is always primitive types. Although their strategy of operation is quite similar. Sparse arrays are used to replace hash maps when the key is of the primitive type. Auto-boxing issue can be easily resolved with SparseArray which is not achievable by ArrayMap. Approach to resolve auto-boxing impacts the memory consumption.


Below code is used for the creation of ArrayMap and fetch keys and values:


SparseArray sparseArray = new SparseArray();
sparseArray.put(1, “Value1”);
SparseLongArray sparseLongArray = new SparseLongArray();
sparseLongArray.put(1, 1L);
SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
sparseBooleanArray.put(1, true);
SparseIntArray sparseIntArray = new SparseIntArray();
sparseIntArray.put(1, 2);


The Comparison
A lag can emerge in the Android application and limit the application performance which is due to continuous memory allocation and de-allocation. Both ArrayMap & SparseArray avoid memory problem avoid the major issue of memory problem with 2 small arrays than single big one.


How SparseArray scores over HashMap:

  1. Use of primitives makes it more memory efficient.
  2. No auto-boxing
  3. Allocation free


The Drawbacks:

  1. It is considerably slow for larger collections.
  2. Its availability is limited to And


Looking forward to responding to your queries and comments regarding ArrayMap and SparseArray in Android. If any information you want to get included I will update this article with it. Generally, when inserts and deletes are not that frequent such that quantity of items remains less than 1000 then SparseArray/ArrayMap are best possible replacement classes.


About Singsys Pte. Ltd. Singsys is a solution provider that offer user friendly solution on cutting edge technologies to engage customers and boost your brand online results from a set of certified developers, designers who prefer optimized utilization of the available resources to align client’s idea with their skillset to reflect it into a Mobile applicationWeb application or an E-commerce solution

You may be interested in following:

Related Posts...