Filtering

Filtering is one of the most popular tasks in the 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.

Filtering 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.

  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf("one", "two", "three", "four")
  4. val longerThan3 = numbers.filter { it.length > 3 }
  5. println(longerThan3)
  6. val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
  7. val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}
  8. println(filteredMap)
  9. //sampleEnd
  10. }

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.

  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf("one", "two", "three", "four")
  4. val filteredIdx = numbers.filterIndexed { index, s -> (index != 0) && (s.length < 5) }
  5. val filteredNot = numbers.filterNot { it.length <= 3 }
  6. println(filteredIdx)
  7. println(filteredNot)
  8. //sampleEnd
  9. }

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 a List<Any>, filterIsInstance<T>() returns a List<T>, thus allowing you to call functions of the T type on its items.
  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf(null, 1, "two", 3.0, "four")
  4. println("All String elements in upper case:")
  5. numbers.filterIsInstance<String>().forEach {
  6. println(it.toUpperCase())
  7. }
  8. //sampleEnd
  9. }
  • filterNotNull() returns all non-null elements. Being called on a List<T?>, filterNotNull() returns a List<T: Any>, thus allowing you to treat the elements as non-null objects.
  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf(null, "one", "two", null)
  4. numbers.filterNotNull().forEach {
  5. println(it.length) // length is unavailable for nullable Strings
  6. }
  7. //sampleEnd
  8. }

Partitioning

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 Lists as a return value: the first list containing elements that match the predicate and the second one containing everything else from the original collection.

  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf("one", "two", "three", "four")
  4. val (match, rest) = numbers.partition { it.length > 3 }
  5. println(match)
  6. println(rest)
  7. //sampleEnd
  8. }

Testing predicates

Finally, there are functions that simply test a predicate against collection elements:

  • any() returns true if at least one element matches the given predicate.
  • none() returns true if none of the elements match the given predicate.
  • all() returns true if all elements match the given predicate. Note that all() returns true when called with any valid predicate on an empty collection. Such behavior is known in logic as vacuous truth.
  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf("one", "two", "three", "four")
  4. println(numbers.any { it.endsWith("e") })
  5. println(numbers.none { it.endsWith("a") })
  6. println(numbers.all { it.endsWith("e") })
  7. println(emptyList<Int>().all { it > 5 }) // vacuous truth
  8. //sampleEnd
  9. }

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.

  1. fun main() {
  2. //sampleStart
  3. val numbers = listOf("one", "two", "three", "four")
  4. val empty = emptyList<String>()
  5. println(numbers.any())
  6. println(empty.any())
  7. println(numbers.none())
  8. println(empty.none())
  9. //sampleEnd
  10. }