In most cases, we don’t have to deal with huge data or processing so by default the thinking runs towards sticking to basic Data Structures and programming but this thinking is wrong.
Android system provides set of collections build especially for mobile development which in turn reflects in apps performance. By using these Data Structures the impact may not be big or prominent at very beginning, but as we know, once the seed is sown, with proper nutrition, right ingredients and care it will become a fruitful tree and these things that we will discuss today are the right ingredients and required nutrition and care that will eventually result into it.
Today will look at some small things and pointers with respect to Android specific Data Structures an Android developer should remember and must try to utilise or implement during application development.
Arrays and Lists
When the number of elements are fixed prefer using Array instead of ArrayList .
ArrayList internally uses Array as default storage method. The internals are simple, it starts with a default length and every time it reaches the max, its size is increased using a right bitwise shift of current value.
int newCapacity = oldCapacity + (oldCapacity >> 1);
The base thumb rule is whenever the length of data is unknown or random access of values is required use ArrayList otherwise look around, better options will be available.
Use LinkedList when linear traversal of data is possible.
Addition and deletion in LinkedList is always constant time.
In case of LinkedList random access of values is a costly operation. If the feature is just to fetch and show list of items to user, then LinkedList should be the preferred choice.
Maps and Sets
Whenever we need to store key-value pair, Generally HashMap is first to strike our mind. Its quite easy and widely used but thats not the case when it comes to Android.
Similar to Arrays use EnumMap over HashMap when the keys are known.
Iterating a HashMap provides no guarantee for order of traversal.
Use LinkedHashMap when same order traversal is required as they were inserted.
Use WeakHashMap as it stores weak references to its keys. Eg: Whenever view references are required to be stored as keys.
Use TreeMap when entries are required to be sorted by keys.
Use Set when the we know that all elements are unique. Similar to HashMap, we have EnumSet and HashSet. When insertion order is to be preserved use LinkedHashSet.
ArrayMap and SparseArrays
When using HashMap, Android Studio itself will warn you that you should use an ArrayMap instead.
Prefer usingArrayMap instead of HashMap.
ArrayMap is a generic key-value mapping data structure that is designed to be more memory efficient than a traditional HashMap. It keeps its mappings in an array data structure — an integer array of hash codes for each item, and an Object array of the key-value pairs.
Always use SparseArray when key is an Integer for key-value data.
SparseArray map Integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices.
It is intended to be more memory efficient than using a HashMap to map Integers to Objects, both because it avoids auto-boxing keys and its data structure doesn’t rely on an extra entry object for each mapping.
There are various SparseArray available. SparseBooleanArray when value is boolean, SparseIntArray when value is Integer, SparseLongArray when value is Long, LongSparseArray when key is of type Long.