Today will look at some small things and pointers w.r.t 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 or make sure to provide the initial length of elements while constructing new ArrayList(SIZE)

instead of or make sure to provide the initial length of elements while constructing 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 by 50%. int newCapacity = oldCapacity + (oldCapacity >> 1);

internally uses 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 by 50%. 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.

otherwise look around, better options will be available. Use LinkedList when linear traversal of data is possible.

when linear traversal of data is possible. Addition and deletion in LinkedList is always constant time.

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.

over when the keys are known. Iterating a HashMap provides no guarantee for order of traversal.

provides no guarantee for order of traversal. Use LinkedHashMap when same order traversal is required as they were inserted.

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.

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.

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 the we know that all elements are unique. Similar to , we have and . 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 using ArrayMap instead of HashMap

instead of 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. Read more about array map here.

is a generic key-value mapping data structure that is designed to be more memory efficient than a traditional . 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. Read more about array map here. Always use SparseArray when key is an Integer for key-value data.

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. Read more about SparseArrays here.

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 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. Read more about SparseArrays here. 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.

Tree, Stack and Queue

We rarely come across a requirement which states clearly that Tree or Stack would be beneficial.

Tree should be preferred whenever there is a case of nested data of similar type. eg: could be community post where people can reply to comments and so on.

should be preferred whenever there is a case of nested data of similar type. eg: could be community post where people can reply to comments and so on. Stack and Queues as the name suggests.

and as the name suggests. Android implements Stack for activity back stack navigation management.

for activity back stack navigation management. Queue can be implemented for cases like batch processing of data or buffering. Eg: Analytics data collection is random and always processed in batches while syncing.

Enums

There are several debates around using integer enums being very costly and one should avoid and instead use integer constants. I may not agree to it but wont completely disagree as well.

Jake Wharton’s tweet says it all.

Whenever in doubt, just follow what legends have been doing (not blindly).

These are just the pointers one should always remember as the side effects of using these are always positive, There is no guarantee though.

As quoted by Matthias Welwarsky ~ Implementation is too late a point to think about performance. If you botch the architecture, no clever coding tricks will save you.