Filter Operator : Kotlin
The collection is something which is used by almost everyone. It makes our life easy. List, Set and Map are the best examples of them.
To iterate, filter or modify the existing collection object, Kotlin provides us with a few in builds transform operators. Filter operator is one of them.
Lets try to understand the filter operator in detail.
This post was originally posted at https://agrawalsuneet.github.io/blogs/filter-operator-kotlin/ and later reposted on Medium.
What is filter operator?
Filter operator is used to create a new collection object by iterating over the existing collection object and filtering the elements based on the predicates to it. The predicate that needs to be checked for, is passed as lambda to the function.
Since the filter operator is an extension of the Iterable Interface, it can be used with all three collections ie List, Set and Map.
/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
Filter also has other few functions as below,
- filterIndexed
used for indexed iteration and filter the elements based on the given predicate. - filterNot
used to filter the elements which did not match the given predicate. - filterNotNull
used for filtering non-null elements. This doesn’t take any predicate. - filterIsInstance
used to filter all the elements which are instance of a particular type. This also doesn’t take any predicate.
/**
* Returns a list containing only elements matching the given [predicate].
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Collections.Filtering.filterIndexed
*/
public inline fun <T> Iterable<T>.filterIndexed(predicate: (index: Int, T) -> Boolean): List<T> {
return filterIndexedTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>>{
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements that are not `null`.
*
* @sample samples.collections.Collections.Filtering.filterNotNull
*/
public fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
return filterNotNullTo(ArrayList<T>())
}
/**
* Returns a list containing all elements that are instances of specified type parameter R.
*
* @sample samples.collections.Collections.Filtering.filterIsInstance
*/
public inline fun <reified R> Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> {
return filterIsInstanceTo(ArrayList<R>())
}
Filter with List
filter operator can be used over a list to check the predicate on its elements and return the filtered list. It can be used with a named parameter or using it.
val list = listOf(1, 2, 3, 4, 5)
val filteredList = list.filter { it % 2 == 0 }
println(filteredList)
val filteredList2 = list.filter { item -> item % 2 == 0 }
println(filteredList2)
val filteredNotList = list.filterNot { item -> item % 2 == 0 }
println(filteredNotList)
In the case of filterIndexed, since we have two variables passed in the lambda function, we can not skip the named parameters.
val list = listOf(1, 2, 3, 4, 5)
val filteredIndexList = list.filterIndexed {index, item -> (index != 0) && (item % 2 == 0) }
println(filteredIndexList)
In the case of filterNotNull, it filter outs the null values and return the list of only the non-null values.
val list = listOf(1, null, 3, 4, 5, null)
val filteredNotNullList = list.filterNotNull()
println(filteredNotNullList)
In the case of filterIsInstance, it filter outs and return the list of the elements of particular data type specified.
val list = listOf(1, "", true, 4, 0.0, 0.0f)
val filteredIsInstanceList = list.filterIsInstance<Int>()
println(filteredIsInstanceList)
Filter with Set
Similar to List, filter operator can be used over a set to check the predicate on its elements return the filtered set. It can be used with a named parameter or using it.