Let ‘s explore some of the often used data structure in Android application development. One of the most important factors that contribute to Android application development is memory, execution speed. Android system provides set of collections build especially for mobile development which in turn reflects in apps performance.
When size is fixed of element use arrays instead of ArrayList because internally ArrayList uses an array by default storage. Every time it reaches its maximum size ArrayList increases its the size to right bitwise shift of current value
newSize=oldSize + oldSize>>1.
Whenever we need to store key-value pair, Generally HashMap is first to strike our mind. Its quite easy and widely used but that’s not the case when it comes to Android. Similar to Arrays use EnumMap over HashMap when the keys are known. Iterating over a HashMap provides no guaranty of the order in which elements will be traversed. Say you are building a shopping cart and store the orders in a map of product -> quantity. The user now wants to see all the items in his cart. Since a HashMap is being used, the card would show all the items but the order would be random. Instead of using a LinkedHashMap is much better here, since you can iterate over it like a normal LinkedList.
Enum in Java is a data type that contains fixed set of constants. When we required the predefined set of values which represents some kind of data, we use ENUM. We always use Enums when a variable can only take one out of a small set of possible values. Each value in an ENUM is an object and each declaration will take some runtime memory simply to reference the object. So ENUM values will take more memory then Integer or String constant.
Be the first to comment.