0
0
Kotlinprogramming~10 mins

Filter and filterNot in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Filter and filterNot
Start with List
Apply filter(predicate)
Keep elements where predicate is TRUE
Result: Filtered List
Start with List
Apply filterNot(predicate)
Keep elements where predicate is FALSE
Result: Filtered List
Filter keeps elements that match the condition (predicate true). FilterNot keeps elements that do NOT match the condition (predicate false).
Execution Sample
Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filter { it % 2 == 0 }
val notEvens = numbers.filterNot { it % 2 == 0 }
println(evens)
println(notEvens)
This code filters even numbers with filter and odd numbers with filterNot from a list.
Execution Table
StepElementPredicate (it % 2 == 0)filter ActionfilterNot Actionfilter OutputfilterNot Output
11falseExcludeInclude[][1]
22trueIncludeExclude[2][1]
33falseExcludeInclude[2][1, 3]
44trueIncludeExclude[2, 4][1, 3]
55falseExcludeInclude[2, 4][1, 3, 5]
End----[2, 4][1, 3, 5]
💡 All elements processed; filter keeps elements with predicate true; filterNot keeps elements with predicate false.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
evens[][][2][2][2, 4][2, 4][2, 4]
notEvens[][1][1][1, 3][1, 3][1, 3, 5][1, 3, 5]
Key Moments - 2 Insights
Why does filter keep elements where the predicate is true, but filterNot keeps elements where the predicate is false?
filter includes elements when predicate returns true (see execution_table rows 2 and 4 where elements 2 and 4 are included). filterNot does the opposite, including elements when predicate returns false (see rows 1, 3, 5 where elements 1, 3, 5 are included).
What happens if the predicate always returns true?
filter will keep all elements, filterNot will keep none. This is because filter includes elements with predicate true, filterNot excludes them.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'evens' after processing element 4?
A[2, 4]
B[2]
C[4]
D[1, 3]
💡 Hint
Check the 'filter Output' column at Step 4 in the execution_table.
At which step does 'notEvens' first include an element?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'filterNot Output' column and see when it changes from empty to non-empty.
If the predicate was changed to 'it > 3', which element would be included first in 'evens'?
A2
B4
C5
D3
💡 Hint
Predicate 'it > 3' is true starting from element 4; check how filter includes elements based on predicate.
Concept Snapshot
filter(predicate): keeps elements where predicate is true
filterNot(predicate): keeps elements where predicate is false
Both return new lists without changing original
Use filter to select matching items, filterNot to exclude them
Predicate is a function returning true/false for each element
Full Transcript
This visual execution shows how Kotlin's filter and filterNot functions work on a list of numbers. Starting with the list [1, 2, 3, 4, 5], each element is tested with the predicate 'it % 2 == 0' which checks if a number is even. filter keeps elements where this predicate is true, so it includes 2 and 4. filterNot keeps elements where the predicate is false, so it includes 1, 3, and 5. The execution table traces each step, showing which elements are included or excluded. The variable tracker shows how the filtered lists build up after each element is processed. Key moments clarify why filter and filterNot behave oppositely. The quiz tests understanding of the step-by-step filtering process. The snapshot summarizes the main points: filter keeps elements matching the condition, filterNot keeps those that don't, both return new lists, and the predicate decides which elements to keep.