Exploring Collections and Sequences in Kotlin
Learn how to deal with collections with respect to context
Working on collections is a common task for developers on any platform. Kotlin provides numerous extension functions to deal with collections. Mainly, it offers two ways to deal with collections: eagerly with collections and lazily with sequences. Read on to learn about them.
I’m not going to judge which is the best out of the two. Instead, I’m going to explain how they work with a simple example so that you can choose the best one based on your context.
Preparing for Heist
To make things interesting, let’s consider a list of heists.
Heist is a data class with four variables, namely a
crew with data types
Crew. Have a look:
data class Heist(val place : String,
val year : Int,
val execute : Boolean,
val crew : Crew)
var lishOfHeists : ArrayList<Heist> = ArrayList()
The crew is another data class in which we have data of the crew members as an Arraylist with the
CrewMember type. Have a look at both the
CrewMember data classes:
data class Crew(val crewList : ArrayList<CrewMember>,
val total : Int)data class CrewMember(val name : String,
val expertise : String)
Enough with the preparations. Let’s get started with the real task here. In the
lishOfHeists, I’ve got a heist item in Position 5 with the year as 2020. Our mission here is to execute the heist with the year 2020.
Executing Heist With Collections
First, let’s do it eagerly with collections. Have a look:
Here, we have two operations:
map (an intermediate transformation) and
first (the terminal operation).
map triggered on
listOfHeists internally, it’ll create another list. Once all the objects in the list are finished iterating in the
map, then it’ll enter into terminal operation
No matter if you have ten or 100 items in the list, when you execute with collections, all the 100 items will run eagerly in the
map. Then the terminal operation will execute.
In case the item that we’re searching is in the fifth position — like with our heist — the map will execute all 100 heists because the
terminal function only executes once all the iterations are done in the intermediate transformation.
The problem is the intermediate transformation is happening to all the items, unnecessarily wasting valuable resources.
Executing Heist With Sequences
Let’s do it lazily with sequences. Have a look:
Unlike collections, sequences won’t execute the intermediate transformation right away. It’ll lazily wait until the
terminal function is triggered, and then it’ll perform both the
terminal functions sequentially.
This way, in the iteration, both the
terminal functions will execute sequentially. So in Position 5, the iteration will terminate as the condition satisfies. This resolves the problem of iterating all the items in the list unnecessarily.
When you’re working on small lists and with no more than two to three transformations, collections will probably be fine. But when you’re working with large lists and complex operations, I would recommend using sequences. Be pragmatic and choose what works for you.
Thank you for reading!