Filtering collections
Filtering is one of the most popular tasks in collection processing. In Kotlin, filtering conditions are defined by predicates – lambda functions that take a collection element and return a boolean value: true
means that the given element matches the predicate, false
means the opposite.
The standard library contains a group of extension functions that let you filter collections in a single call. These functions leave the original collection unchanged, so they are available for both mutable and read-only collections. To operate the filtering result, you should assign it to a variable or chain the functions after filtering.
Filter by predicate
The basic filtering function is filter()
. When called with a predicate, filter()
returns the collection elements that match it. For both List
and Set
, the resulting collection is a List
, for Map
it's a Map
as well.
The predicates in filter()
can only check the values of the elements. If you want to use element positions in the filter, use filterIndexed()
. It takes a predicate with two arguments: the index and the value of an element.
To filter collections by negative conditions, use filterNot()
. It returns a list of elements for which the predicate yields false
.
There are also functions that narrow the element type by filtering elements of a given type:
filterIsInstance()
returns collection elements of a given type. Being called on aList<Any>
,filterIsInstance<T>()
returns aList<T>
, thus allowing you to call functions of theT
type on its items.fun main() { //sampleStart val numbers = listOf(null, 1, "two", 3.0, "four") println("All String elements in upper case:") numbers.filterIsInstance<String>().forEach { println(it.uppercase()) } //sampleEnd }filterNotNull()
returns all non-nullable elements. Being called on aList<T?>
,filterNotNull()
returns aList<T: Any>
, thus allowing you to treat the elements as non-nullable objects.fun main() { //sampleStart val numbers = listOf(null, "one", "two", null) numbers.filterNotNull().forEach { println(it.length) // length is unavailable for nullable Strings } //sampleEnd }
Partition
Another filtering function – partition()
– filters a collection by a predicate and keeps the elements that don't match it in a separate list. So, you have a Pair
of List
s as a return value: the first list containing elements that match the predicate and the second one containing everything else from the original collection.
Test predicates
Finally, there are functions that simply test a predicate against collection elements:
any()
returnstrue
if at least one element matches the given predicate.none()
returnstrue
if none of the elements match the given predicate.all()
returnstrue
if all elements match the given predicate. Note thatall()
returnstrue
when called with any valid predicate on an empty collection. Such behavior is known in logic as vacuous truth.
any()
and none()
can also be used without a predicate: in this case they just check the collection emptiness. any()
returns true
if there are elements and false
if there aren't; none()
does the opposite.