Beyond simple filtering and mapping of your collections, Kotlin provides several functions which allow you to aggregate your data as well. In this video, learn how to make use of the following functions: min, minBy, maxBy, and sum.
- [Instructor] Beyond simple filtering and mapping of your collection data, Kotlin provides several functions which allow you to aggregate your data as well. Let's take a look at the aggregate operations file so that we can get some deeper insight into our collection of planets. So our first function is "fewestMoons" here on line 11. And since each planet has a given number of known moons, we want to get the smallest number. Now we can do this by first getting the list which contains the number of known moons only.
So we'll just create a variable, we'll set it equal to "moons", we'll get our list of planets and then we'll call the map function on it and let's provide our "Planet", "knownMoons" and the member reference. Perfect. So let's come here on line 16 and print this list out first. And then we can duplicate that and we're going to call the min function in order to get the smallest number of known moons.
So if we run this test, what we would hope to see is first our list of all the total number of moons for each planet and then the minimum number. And that's exactly what we get here. So we have zero, zero, one, all the way up to our final planet which has 14 known moons and then of course the minimum number is zero. Now let's say we want to know, well, which planet has zero moons? That's going to bring us to our next function, so let's close this down. The planet with the fewest moons.
And to do that, we're going to create a variable which we'll call "planet" and let's set that equal to our planets list and this time we're going to take advantage of the minBy function. Now this is going to take in the lambda with the criteria that we're interested in and that is the known moons. So let's go ahead and print this out. And first we'll print the name of the planet and then let's follow that up with...
Its moons. So we'll just go "planet?.knownMoons" and then "moons". And that's all we need, so we can run this test. And we get Mercury, which has zero moons. Now if you recall from our previous example, there were actually two planets that had zero known moons. So why is it that we only get one, Mercury? We'll that's because the minBy function would just grab the first element that yields the smallest value.
So that's something important that you want to be aware of whenever you're working with this particular function. Now let's go ahead and do the inverse. So we'll close that down. Copy our same code from lines 22 to 24 and we'll paste it into the "planetwithMostMoons" function and all we have to do is just a simple change of minBy to maxBy. And let's run this one.
And we end up with Jupiter, which has 69 known moons, which is just amazing. So finally, let's get the total number of all known moons and for this one, we're going to use the sumBy function. So let's close this down, we're going to take our same little code here on line 29 and we'll go to our final function, "totalMoons", just paste it in. So let's rename this variable to "moons", cause it's now going to be a number and then instead of maxBy, we'll you sumBy.
And let's go ahead and print that out here on line 38. We're going to do a "$moons" and then "moons". Okay, so let's run this test. And we get 175 known moons for our solar system. Working with your data in Kotlin is very intuitive and leads to code that is easy to understand. I hope you've got a better sense of how you can take advantage of these types of aggregate operations when you're working with your collections.
- 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