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!