From the course: Intermediate Kotlin for Android Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Collection operators: Mapping

Collection operators: Mapping

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Collection operators: Mapping

- [Instructor] Transforming data is a common requirement for most modern Android apps. Kotlin helps us out by providing functions in the standard library for transforming our collection data. Let's say we have a list of users, but we actually need a list of their names. One approach could be to create a new list, then iterate over the existing list and add the users' names. But Kotlin offers us a better way. We can use the map function and provide a lambda, which contains the transformation that we're interested in. Notice that we're using our familiar it variable. This represents a given instance of a user, and then we simply grab their name. This results in a new list, which contains the name of each user. We can even rewrite this using member references and get the same result. Another useful function for mapping your data is the groupBy function. Let's say you have a list of users and you want to divide them into groups based on whether or not they can legally drive. Using the…

Contents