Kotlin Collections: Getting Started

In this tutorial, you’ll learn how to work with Kotlin Collections. You’ll transform data, filter it out, and use different types of collections in Kotlin! By Filip Babić.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Looking Up Data

You also have various ways to look up data:

  val receiptByIndex = receipts[0] // receipt.get(0)
  val firstPaidReceipt = receipts.first { it.isPaid } // will crash if there is none
  val firstPaidReceiptOrNull = receipts.firstOrNull { it.isPaid } // either is paid, or null
  val lastByPredicate = receipts.last { !it.isPaid } // last which is not paid

These functions help you look up data beyond indices, as you can add different logic to the conditions you’re looking for.

Where To Go From Here

Congratulations! You’ve learned the basics of Kotlin collections.

As you can see by the length of this tutorial, Kotlin collections is a huge topic! And even though you learned a lot along the way, there is still much more to cover.

If you’re hungry for more, you may want to check out the official documentation to learn about Sequences.

There are also a few more examples in the projects, so be sure to download them using the Download Materials button, on top or the bottom of the page. Please join the discussion in the comments and the forum and ask any questions you have.

Hope you have a lovely time working with Kotlin collections!