From the course: Android Development Tips
Filter data collections with Kotlin - Android Tutorial
From the course: Android Development Tips
Filter data collections with Kotlin
- [Instructor] In last week's tip, I described how to sort and calculate the values of collections. Now, I'll show you how to filter collections and how to combine that with sorting and other functions. I'm once again working with two different collections, stateList is a list of strings and shoppingCart is a list of complex objects. Instances of the class LineItem, which has references to the class ClothingItem. I'll start with a simple filter. Let's say I wanted to create a filtered list. I'll create a new variable and I'll call it filteredList. I'll get its value by calling stateList and then from there, I'll call the function filter. This is asking for a lambda expression and specifically this function is looking for a Boolean value. Whatever code I pass in is going to be executed against each item in the list. If my code returns true, the item will be included in the filtered list and if it's false, it won't be included. I'll use the expression it.startsWith and then the uppercase M. Then, as I've done before, I'll use a simple loop and this time I'm going to loop through filteredList and I'll output the name of the state. Now, I'm only listing states that start with the letter M, but you can actually combine filtering with other functions. For example, if I also wanted to sort the data, I can simply chain the calls together. The filter function returns a reference to this list, so from here I can call sortedBy, which I demonstrated last week, and this time instead of sorting alphabetically, I'm going to sort by the length of the string with it.length. I'll run the code again and this time I'm filtering and sorting the data, and so when the data is displayed, the shortest state name will be displayed first and the longest last. There's the result. This filtering strategy can be applied to collections containing complex objects just as easily. In the lambda expression, just use whatever expression you need to reference the property on which you want to filter. For example, I'll go down here to where I'm working with my shoppingCart and I'll create a variable called filteredCart. I'll start by calling the shoppingCart object and from there I'll call the filter function. For the lambda expression, I'll pass in it.clothingItem.clothingType equals clothingItem.SHIRT. Now, I know because I built the code that there's only going to be one item that satisfies that requirement. My filteredCart will have one and only one item in it. I'll get a reference to that LineItem with filtered.Cart.get and I'll pass in a value of zero. Alternatively, you can use indexing syntax and it'll look like it's an array. Then, I'll output the item using my log function and I'll pass in item.toString. Now, the LineItem class is a data class and in Kotlin, the data class has a special feature. It generates a two-string function automatically. This will result in highly readable debug output that I can use to find out what really happened when I ran the application. There's the result. I've retrieved one item from the shopping cart and it's the item that is a shirt. That's how easy it is to filter data collections in Kotlin. You don't need to write a lot of looping code and use a lot of Boolean evaluations. Instead, just use the functions that are parts of the collection interfaces, list, set, and map.
Contents
-
-
-
(Locked)
Communicate with Snackbar messages6m 15s
-
(Locked)
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
(Locked)
Convert a layout to ConstraintLayout7m 13s
-
(Locked)
Manage global data with singletons5m 37s
-
(Locked)
Convert image files to WEBP format6m 5s
-
(Locked)
Create an Espresso user interface test5m 18s
-
(Locked)
Create classes for Android with Kotlin5m 12s
-
(Locked)
Run background tasks in intent services8m 51s
-
(Locked)
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
(Locked)
Improve forms with design layouts7m 55s
-
(Locked)
Display draggable rows in a list of data8m 1s
-
(Locked)
Manage persistent data with Realm10m 23s
-
(Locked)
Play an audio file from assets8m 56s
-
(Locked)
Send an SMS message from an app7m 56s
-
(Locked)
Get a list of audio files on a device8m 32s
-
(Locked)
Play sounds with SoundPool8m 45s
-
(Locked)
Display the build date of an APK file6m 47s
-
(Locked)
Build Kotlin classes with properties7m 32s
-
(Locked)
Manage versioning with Gradle variables6m 8s
-
(Locked)
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
(Locked)
Make asynchronous calls with Retrofit4m 6s
-
(Locked)
Send request parameters with Retrofit4m
-
(Locked)
Upgrade a project to Android Studio 3.04m 47s
-
(Locked)
Clean up calls to findViewById()2m 58s
-
(Locked)
Create adaptive launcher icons5m 47s
-
(Locked)
Use XML and downloadable fonts6m 40s
-
(Locked)
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
(Locked)
Manage an SQLite database with Room9m 17s
-
(Locked)
Manage background threads with Executors5m 32s
-
(Locked)
Track vital stats in a published app3m 4s
-
(Locked)
Inspect database files with DB Browser4m 41s
-
(Locked)
Execute tasks with JobScheduler7m 29s
-
(Locked)
Create Room entity classes in Kotlin8m 22s
-
(Locked)
Create Room DAO classes in Kotlin4m 13s
-
(Locked)
Upgrade to Android Studio 3.13m 22s
-
(Locked)
Convert a project to use Kotlin3m 20s
-
(Locked)
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
(Locked)
Define global constants with Kotlin6m 12s
-
(Locked)
Use Kotlin data and enum classes5m 24s
-
(Locked)
Create inheritance hierarchies in Kotlin6m 8s
-
(Locked)
Manage data collections in Kotlin5m 45s
-
(Locked)
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
(Locked)
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
(Locked)
Use virtual devices with Google Play3m 50s
-
(Locked)