0
0
Kotlinprogramming~15 mins

Filter and filterNot in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter and filterNot
📖 Scenario: You are managing a list of products in a store. Some products are on sale, and you want to separate the sale items from the regular items.
🎯 Goal: Build a Kotlin program that uses filter and filterNot to create two lists: one with products on sale and one with products not on sale.
📋 What You'll Learn
Create a list of products with their sale status
Create a variable to hold the sale status to filter by
Use filter to get products on sale
Use filterNot to get products not on sale
Print both filtered lists
💡 Why This Matters
🌍 Real World
Filtering lists is common when you want to separate data based on conditions, like showing only available products or filtering messages.
💼 Career
Understanding how to filter collections is important for data processing, user interface updates, and backend logic in many programming jobs.
Progress0 / 4 steps
1
Create the product list
Create a list called products with these exact entries: "Shoes" to true, "Hat" to false, "Jacket" to true, "Socks" to false.
Kotlin
Need a hint?

Use listOf with pairs like "Name" to Boolean.

2
Set the sale filter
Create a Boolean variable called onSale and set it to true.
Kotlin
Need a hint?

Just write val onSale = true.

3
Filter products on sale and not on sale
Create a list called saleItems by filtering products where the second value equals onSale. Then create a list called regularItems by filtering products where the second value does NOT equal onSale using filterNot.
Kotlin
Need a hint?

Use filter and filterNot with it.second == onSale.

4
Print the filtered lists
Print saleItems and regularItems each on its own line.
Kotlin
Need a hint?

Use println(saleItems) and println(regularItems).