Kotlin Collections
In this article, we explore the key types of Kotlin collections — List, Set, and Map — along with their mutable counterparts, providing real-life examples, time complexities, and practical tips and tricks for effective usage.
1. Lists in Kotlin:
Lists are ordered collections of elements, and Kotlin provides a wealth of operations for accessing, iterating, and transforming them. Whether you’re dealing with product listings in an e-commerce application or managing tasks in a to-do list, Lists offer a versatile solution. Tips include using firstOrNull for conditional element retrieval and employing distinct to remove duplicates.
🗒 list: A collection of elements in a specified order.
🗒 A List in Kotlin is an ordered collection of elements. The elements in a list have indices, and you can access them by their position.
👉 numbers is a list of integers. You can access elements using indices, iterate through the list, and use higher-order functions like map to transform the elements.
🚀 Real-life problem: In a shopping application, you need to display a list of products and allow users to filter products based on their preferences, such as price range.
👉 productList is a list of Product objects. Using the filter function, you can easily extract products that fall within a specific price range.
⏰ The time complexity
- Accessing an element by index (list[index]): O(1)
- Iterating through the list with a loop (for (element in list)): O(n)
- Filtering elements (list.filter { condition }): O(n)
- Adding an element to the end of the list (list.add(element)): O(1)
🎃 Tips and Tricks
Tip 1: Use firstOrNull or find for finding the first element that satisfies a condition:

Tip 2: Use distinct to get a list with duplicate elements removed:

Tip 3: Use subList for extracting a portion of the list:

2. Sets in Kotlin:
Sets, unordered collections of unique elements, find applications in scenarios where distinctiveness is crucial, such as managing likes on a social media post. Understanding set operations like union, intersect, and subtrack can streamline set manipulations. Additionally, converting a List to a Set using toSet proves to be a useful trick.
🗒 set : A collection of unique elements, with no specific order.
🗒 A Set is a collection of unique elements. Unlike a list, a set doesn't allow duplicate values, and the order of elements is not guaranteed.
👉 uniqueNumbers is a set containing integers. You can add and remove elements using the + and — operators, and you can check for the existence of an element using the in operator.

🚀 Real-life problem: In a social networking application, you want to maintain a list of unique likes for a post.
👉 likedUsers is a set of user IDs who liked a particular post. The set ensures that each user can like the post only once, avoiding duplicates.
⏰ The time complexity
- Adding an element (set.add(element)): O(1)
- Removing an element (set.remove(element)): O(1)
- Checking if an element exists (element in set): O(1)
🎃 Tips and Tricks
Tip 1: Use union, intersect, and subtract for set operations:

Tip 2: Use toSet to convert a list to a set:

3. Maps in Kotlin:
For key-value pairs, Maps provide an efficient solution, ideal for scenarios like user information storage. Techniques like using getOrElse for default values and transforming Lists of pairs into Maps using toMap enhance the versatility of Map operations.
🗒 map: A collection of key-value pairs.
🗒 A Map is a collection of key-value pairs. Each key in a map is associated with a specific value.
👉 userMap is a map where keys are strings (e.g., “username”) and values are also strings (e.g., “john_doe”). You can access values using keys, and you can iterate through key-value pairs.
🚀 Real-life problem: In a user management system, you want to store user information and quickly retrieve details based on the user’s username.
👉 userMap is a map where the keys are usernames, and the values are UserInfo objects. This allows for efficient retrieval of user details based on the username.
⏰ The time complexity
- Accessing a value by key (map[key]): O(1) on average, but can be affected by the hash function and potential collisions.
- Iterating through key-value pairs (for ((key, value) in map)): O(n)
- Adding a key-value pair (map[key] = value): O(1)
- Removing a key-value pair (map.remove(key)): O(1)
🎃 Tips and Tricks
Tip 1: Use getOrElse to provide a default value for a non-existent key:

Tip 2: Use toMap for converting a list of pairs into a map:

4. Mutable Collections in Kotlin:
Mutable counterparts of Lists, Sets, and Maps offer dynamic modification capabilities. Whether you’re adding, removing, or updating elements in a to-do list or dynamically altering user information in a map, understanding operations like addAll, removeAll, and sort can significantly improve the efficiency of your code.
🗒 Kotlin provides mutable versions (e.g., MutableList, MutableSet, MutableMap) that allow you to modify their content.
👉 mutableList is a mutable list that you can modify by adding, removing, or updating elements.
🚀 Real-life problem: In a to-do list application, users need the ability to add, remove, and update tasks.
👉 taskList is a mutable list that allows for dynamic modification of tasks, including adding, removing, and updating tasks.
⏰ The time complexity
- Adding an element to the end (mutableList.add(element)): O(1) on average, but resizing the underlying array may take O(n) occasionally.
- Removing an element by value (mutableList.remove(element)): O(n) because it may need to search for the element.
- Updating an element by index (mutableList[index] = newValue): O(1)
🎃 Tips and Tricks
Tip 1: Use addAll to add multiple elements to a mutable collection at once:

Tip 2: Use removeAll or retainAll to modify a collection based on another collection:

Tip 3: Use sort for sorting a mutable list in-place:


on freeicons.io
Stackademic
Thank you for reading until the end. Before you go: