This content originally appeared on DEV Community and was authored by Sanjay Prajapat
Filtering conditions are defined by predicate and lambda function takes collection element . it return true when given element matches to predicate , return false if doesn't matches.
Filter by predicate
use filter() to filtering. it returns collection element that matches with predicate. for list and set resulting collection is list, for map it returns map.
fun main(args: Array<String>) {
val numbers1 = listOf(1,2,3,4,5,6)
val numbers2 = listOf(0,5,3,4,3,2)
// return list of elemenet that if number1's elements present in number2 list
val filterNum = numbers1.filter { predicate ->
numbers2.contains(predicate)
}
print(filterNum) //[2, 3, 4, 5]
val numMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
val filterMap = numMap.filter { (key ,value) ->
key.startsWith("k") && value >2
}
print(filterMap) // {key3=3, key11=11}
}
filterIndexed() to use elements position
fun main(args: Array<String>) {
val numbers1 = listOf(1,2,3,4,5,6)
val filterIndexed = numbers1.filterIndexed {
index,element -> (index>1) && element > 2
}
print(filterIndexed) // [3, 4, 5, 6]
}
filterNot() to filter collection on false condition ,
fun main(args: Array<String>) {
val numbers1 = listOf(1,2,3,4,5,6)
val f = numbers1.filterNot {
it > 3
}
print(f) // [1, 2, 3]
}
filterIsInstance() allow us to call functions of T type on its items
fun main(args: Array<String>) {
val numbers3 = listOf("1",null, "Hello", "World","3.20")
val filter3= numbers3.filterIsInstance<String>().forEach {
print(it.uppercase()+ " " ) // 1 HELLO WORLD 3.20
}
print(filter3) // kotlin.Unit
}
filterNotNull() returns all non-null elements
fun main(args: Array<String>) {
val numbers4 = listOf("1",null, "Hello", "World","3.20")
val filter4 = numbers4.filterNotNull().forEach{
print(it + " ") // 1 Hello World 3.20
}
println(filter4) // kotlin.Unit
}
Partition
it returns pair of list , first melements are matches in seperate and that are not matched goes in another list
val numbers5 = listOf("one", "two", "three", "four", "five")
val (matched, rest) = numbers5.partition { predicate -> predicate.length>3 }
println(matched) // [three, four, five]
println(rest) // [one, two]
Test predicates
these are function used for test a predicate against element
any() : It returns true if at least one element matches
none() : It returns true if no elements matches with given predicate , if matches then return false
all() : It returns true if all element matches with given predicate
println(numbers5.any{ it.startsWith("t")}) // true
println(numbers5.none{ it.startsWith("o")}) //false
println(numbers5.all { it.length > 3}) // false
// all() returns true for any predicate for emptylist
println(emptyList<Int>().all { it > 5 }) // true
any() ,none() for without predicate
any() - return true if elements in list , false for empty list
none() -> return true if elements present in list , true for empty list
println(numbers5.any()) // true
println(emptyList<Int>().any()) // false
println(numbers5.none()) // false
println(emptyList<Int>().none()) // true
This content originally appeared on DEV Community and was authored by Sanjay Prajapat
Sanjay Prajapat | Sciencx (2021-09-04T19:22:00+00:00) Filtering Collection In Kotlin. Retrieved from https://www.scien.cx/2021/09/04/filtering-collection-in-kotlin/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.