W3cubDocs

/Kotlin

filter

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun <T> Sequence<T>.filter(
    predicate: (T) -> Boolean
): Sequence<T>

Returns a sequence containing only elements matching the given predicate.

The operation is intermediate and stateless.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val numbers: List<Int> = listOf(1, 2, 3, 4, 5, 6, 7)
val evenNumbers = numbers.filter { it % 2 == 0 }
val notMultiplesOf3 = numbers.filterNot { number -> number % 3 == 0 }

println(evenNumbers) // [2, 4, 6]
println(notMultiplesOf3) // [1, 2, 4, 5, 7]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/filter.html