Common Data Structures for Mobile Development with Examples
If you have ever wondered whether or not you need to master a red-black tree or a max heap to become a proficient mobile developer, the answer is, probably not. In fact, you may never use either of them throughout your career!
Now I don’t mean to say you shouldn’t learn any data structures outside of the ones I will list in this post, you should definitely be aware of them because you never know when they may come in handy. However, our time is limited, so if you want to know which ones you should focus on the most for mobile development, keep reading!
Usually one of the first data structures (DS) learned by developers, arrays are probably the most common DS found in mobile apps. The reason is simple: most apps work with collections of data.
Naturally, arrays are the simplest way to store a collection of data. They are simple to work with and there are a plethora of algorithms that make use of them. A simple example is sorting. Usually it is best to present data to users sorted by a specific property, for example, by name or date. This task is simplest if using an array.
Languages such as Swift (iOS) and Kotlin (Android) have built in methods for sorting, so you don’t need to implement any algorithms yourself. Below is an example demonstrating how the built in array functions can be used to sort items in a shopping cart.
Here is the equivalent code in Kotlin.
Additionally, if you have data stored in an array, not only is it easy to sort, it is also easy to manipulate for other common tasks such as filtering. When presenting users with a list of data, it is common to allow them to select and apply filters to narrow down their results. One simple way of applying filters on an array of data is to simply iterate the entire array and build a new one containing only items that match the filter criteria.
Just like for sorting, the arrays in Swift and Kotlin also have built in methods for filtering.
The next most common DS I've seen in mobile apps is the Dictionary, AKA a map, depending on what language you are using (we will use “dictionary” moving forward). Dictionaries contain a collection of key-value pairs which makes them very efficient at accessing a value quickly by using its corresponding key.
There is no need to iterate a dictionary looking for a value, you can simply use the corresponding key for the desired value. This is obviously very useful when you need to quickly check if a value exists in a collection or need quick access to it. This does however, come with a trade-off, which is it requires more memory.
It would be a great idea to familiarize yourself with how dictionaries are implemented so that you understand the trade-offs and know when it is appropriate to use one. Modern mobile devices usually come with sufficient amount of memory so it isn't usually a big issue, but still good to be aware of.
Below is an example of how a dictionary can be used to quickly look up the price of an item.
As you can see from the above examples, using a dictionary makes it simple and fast to look up a value. The examples above could also be accomplished with an array, but then we would need to iterate the entire array to find the appropriate value.
Oftentimes, apps contain predefined, finite data sets that they use in their business logic. Take for example, the different states that an object can be in:
On, Off, Charging
Low, Medium, High
Pending, Approved, Declined
All of these sets of data are usually best represented by an Enumeration (enum). Using an enum ensures that only values from the set are used. This is checked during compile time. Further, enums in Swift and Kotlin have additional useful features that make enums even more powerful.
In the below examples, I demonstrate how the status of a transaction can be represented using an enum. Additionally, I demonstrate how language features in both Swift and Kotlin can be used to make enums even more useful.
Oftentimes there is a need for collections containing unique values only. For example, take a favorite songs playlist, you normally don’t want to allow a song to appear in the playlist more than once. Using a set guarantees each value contained within it is unique.
The set DS can be implemented using an array, or a Dictionary, or other data structures. However, Swift and Kotlin both have built in implementations with very useful features.
In both Swift and Kotlin, the set DS makes it easy to perform set operations such as union, intersection, and subtraction.