How to use efficiently data Structure in Android development

Updated 7 May 2018

Save

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.

Arrays vs Lists

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.

Maps vs Sets

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.

Enums vs Integer Constants

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.

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home