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: Filtering

Collection operators: Filtering

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Collection operators: Filtering

- [Instructor] Kotlin provides several functional APIs for manipulating collections. These help you to simplify your code and handle common tasks without needless boilerplate. For example, there are many functions which aid in filtering your data. Let's say we have a list of users, aged 35, 25, and 14. And we only want those who are old enough to drive. One approach could be to create a new list, then iterate over the existing list, check for the age we want, and then add the user to our new list. But this is just boilerplate code that somewhat obscures what we're trying to achieve. So instead of that, we can use the filter function and provide the lambda which we want to check against. Notice that we're using our familiar it variable. This is going to represent a given instance of a user and then we compare their age to the number 16. This is going to return to us our desired list of users, Nate and Andrea. At the same time, it leaves our original list of users intact. In fact…

Contents