October 2018
Intermediate to advanced
370 pages
9h 15m
English
This is the most commonly used function in Kotlin. The filter function takes one lambda expression, which takes one variable as an input and returns the same variable as a result. The filter function applies the lambda expression and returns a list as a result. Let's create a list of integers and apply some filters:
var numbers = listOf<Int>(1,2,3,4,5,6,7,8,9,10)
We can filter out all elements that are greater than five as follows:
var newList = numbers.filter{ i -> i > 5 }println("Filter out greater than 5")println(newList)
i represents the element in the list and i > 5 is a condition that filters the results. The lambda expression filters the elements and the filter function returns a new list.
We can filter out all ...
Read now
Unlock full access