Exploring Collections and Sequences in Kotlin

Learn how to deal with collections with respect to context

Siva Ganesh Kantamani
Mar 18 · 3 min read
Photo by Clem Onojeghuo on Unsplash.

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

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 Crew and 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

Here, we have two operations: map (an intermediate transformation) and first (the terminal operation).

When the 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 first.

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

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 intermediate and terminal functions sequentially.

This way, in the iteration, both the intermediate and 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.


Conclusion

Thank you for reading!

Better Programming

Advice for programmers.

Siva Ganesh Kantamani

Written by

Learn. Code. Write. Repeat. Visit me at https://about.me/sivaganesh_kantamani & Join my email list at https://tinyletter.com/Siva_Ganesh_Kantamani

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade