Kotlin provides several functional APIs for manipulating collections. These help you to simplify your code and handle common tasks without needless boilerplate. In this video, learn about different ways you can filter your data.
- [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, Kotlin has many functions that you can use to filter your data. Here's just a sampling of a few. filterNot, which lets you get elements that don't match what's in your lambda. Take and drop, these both return subsets of your data based on a number that you give as input. Let's switch over to Android Studio, so we can see filtering in action. This is the main screen of our sample act. And we have a few menu options available.
Show all, show only those planets which are terrestrial, or only gas giants. If we look at our planet list activity here in Android Studio, we have the implementation for the show all option already available here on lines 91 through 93. It just gets the list of items, updates our adapter, and returns true. What we want to do is add the implementation for the remaining items. Filter terrestrial and filter gas giant.
So let's start by copying what we have here for filter none, and let's paste that down here on line 97, for our next filter. Since we only want to show planets that are terrestrial, we're going to update line 97 by using the filter function. So we take our list of items, we're going to use filter, and for our lambda, we'll check that our planet is an inner planet. That's all we need to do. This is going to pare down our resulting list.
So let's run our application, and once that's loaded, we can check out our new filter. So we'll switch back over to our emulator, go to our menu, and let's choose only terrestrial. Notice that this reduced the number of planets available to only those that are inner planets. Now let's go back to our activity, and we can add our final filter. We're going to copy the same thing that we had before, and we're going to paste that inside of the filter gas giants code here on line 103.
But we're going to make one modification, and that's here on line 103. Instead of using filter, we're going to use the filter not function. This is because it's going to do the inverse and look for planets that have their inner property set to false. And that's all we need to do. So we can go ahead and run the application again. Switch back to our emulator. And this time, we're going to click on only gas giants. And we get a completely different list of planets.
Filtering our collections with Kotlin is simple, and leads to concise code. I encourage you to explore some of the other filter functions that are available.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections