Android App Optimization With ArrayMap and SparseArray

3 minutes read

Array Map and Sparse App

HashMap is quite flexible hence it is most preferred data structure choice to store key -> value pairs. Although Android Studio will war you when you try t use a HashMap. Well then let’s look at why we should be using ArrayMap.

 

HashMap or ArrayMap

 

ArrayMap provide support for the lower android versions as it comes in the package android.util.ArrayMap and android.support.v4.util.ArrayMap. ArrayMap is a generic key -> value mapping data structure specifically designed to be more memory efficient than a traditional HashMap. ArrayMap avoids creation of an extra object for every entry added to the map as it store its mapping in an array data structure which is an integer array of hash codes for every item along with an Object array of the key -> value pairs.

 

This video will provide you further details.

 

 

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

 

When HashMap is queried for the value for a key it comes at a cost of more than usual memory space. Major drawbacks that lingers with it being as on autoboxing extra objects are created with each insertion. and it will adversely impact the garbage collection as well as memory usage. Each time when HashMap is expanded rearrangement of buckets take place which is quite costly affair that grows with number of objects.

 

As far as Android is concerned memory is of key importance specifically for responsive applications and regular allocation and de-allocation of memory along with garbage collection will make android application lag.

 

Now in case of Array Map which uses 2 arrays. ArrayMap uses Object[ ] mArray to store objects and the int[] mHashes to store hashCodes. In ArrayMap the time complexity might increase  to O(logN) from O(1)  but it’s much more memory efficient. a dataset of around 100 records can easily be handled  with no noticeable lag. But you’ll be taking advantage of a much more memory efficient application.

 

I would rather recommended you the following data-structures:

– ArrayMap<K,V> in place of HashMap<K,V>

– ArraySet<K,V> in place of HashSet<K,V>

– SparseArray<V> in place of HashMap<Integer,V>

– SparseBooleanArray in place of HashMap<Integer,Boolean>

– SparseIntArray in place of HashMap<Integer,Integer>

– SparseLongArray in place of HashMap<Integer,Long>

– LongSparseArray<V> in place of HashMap<Long,V>

You might be interested in our previous articles
1. How to Speed Up Android Emulator
2. React or Vue Which Javascript Library is More Useful
3. How to Optimise Speed of Magento Website
4. iOS Development Trends in 2017 beyond Tips and Tricks

Related Posts...

Technologies