What if you could pick or skip items from a list with just one simple line of code?
Why Filter and filterNot in Kotlin? - Purpose & Use Cases
Imagine you have a big list of fruits, and you want to pick only the red ones or exclude the green ones. Doing this by checking each fruit one by one and writing separate code for each case can be tiring and confusing.
Manually checking each item means writing lots of repetitive code. It's easy to make mistakes, like forgetting some fruits or mixing conditions. Also, if the list changes, you have to rewrite or fix your code again and again.
Using filter and filterNot lets you quickly and clearly pick items that match or don't match a condition. This makes your code shorter, easier to read, and less error-prone.
val redFruits = mutableListOf<Fruit>() for (fruit in fruits) { if (fruit.color == "red") { redFruits.add(fruit) } }
val redFruits = fruits.filter { it.color == "red" }You can easily create new lists by including or excluding items based on any condition, making data handling smooth and clear.
Suppose you have a list of tasks and want to see only the completed ones or only the ones not done yet. Using filter and filterNot helps you get those lists instantly without extra loops.
Manual filtering is slow and error-prone.
filter and filterNot simplify selecting items by conditions.
They make your code cleaner, shorter, and easier to maintain.
Practice
filter function do in Kotlin collections?Solution
Step 1: Understand the purpose of
Thefilterfilterfunction selects elements that satisfy a given condition (true).Step 2: Compare with other options
Options B and D describe different operations, and C describesfilterNot, notfilter.Final Answer:
Keeps items where the condition is true -> Option DQuick Check:
filter= keep true items [OK]
- Confusing filter with filterNot
- Thinking filter removes all items
- Mixing filter with sorting functions
numbers in Kotlin?Solution
Step 1: Identify the condition for even numbers
Even numbers satisfyit % 2 == 0.Step 2: Choose correct filter syntax
filterkeeps items where condition is true, sonumbers.filter { it % 2 == 0 }keeps even numbers.Final Answer:
numbers.filter { it % 2 == 0 } -> Option CQuick Check:
filter with even condition = numbers.filter { it % 2 == 0 } [OK]
- Using filterNot instead of filter
- Using division instead of modulo
- Confusing condition signs (== vs !=)
val nums = listOf(1, 2, 3, 4, 5)
val result = nums.filterNot { it > 3 }
println(result)Solution
Step 1: Understand filterNot condition
filterNot { it > 3 }keeps items whereit > 3is false, so items ≤ 3.Step 2: Apply condition to list
Numbers 1, 2, 3 are ≤ 3, so they are kept; 4 and 5 are removed.Final Answer:
[1, 2, 3] -> Option AQuick Check:
filterNot > 3 = keep ≤ 3 [OK]
- Confusing filterNot with filter
- Including numbers greater than 3
- Expecting empty list output
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filterNot { it % 2 == 0 }
println(evens)Solution
Step 1: Analyze the filterNot condition
filterNot { it % 2 == 0 }keeps items whereit % 2 == 0is false, so it keeps odd numbers.Step 2: Check intended output
The code tries to get even numbers but actually keeps odd numbers, so logic is reversed.Final Answer:
It incorrectly keeps odd numbers instead of even -> Option BQuick Check:
filterNot with even condition keeps odd [OK]
- Using filterNot instead of filter for positive condition
- Assuming filterNot removes items instead of keeping false
- Misreading modulo condition
val words = listOf("apple", "banana", "", "cherry", ""), which Kotlin expression returns a list of only the non-empty words using filter and filterNot correctly?Solution
Step 1: Identify condition for non-empty strings
Non-empty strings satisfyit.isNotEmpty().Step 2: Choose correct filter usage
filterkeeps items where condition is true, sowords.filter { it.isNotEmpty() }returns only non-empty words.Final Answer:
words.filter { it.isNotEmpty() } -> Option AQuick Check:
filter with isNotEmpty = keep non-empty [OK]
- Using filterNot with isNotEmpty to keep empty strings
- Confusing isEmpty and isNotEmpty
- Expecting filterNot to remove empty strings
Ready to prove it?
Pass the test and this pattern moves unknown
