filter
@ExperimentalUnsignedTypes inline fun ULongArray.filter(
predicate: (ULong) -> Boolean
): List<ULong>
(source)
@ExperimentalUnsignedTypes inline fun UByteArray.filter(
predicate: (UByte) -> Boolean
): List<UByte>
(source)
@ExperimentalUnsignedTypes inline fun UShortArray.filter(
predicate: (UShort) -> Boolean
): List<UShort>
(source)
Returns a list containing only elements matching the given predicate.
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
}
Returns a new map containing all key-value pairs matching the given predicate.
The returned map preserves the entry iteration order of the original map.
import kotlin.test.*
import java.util.*
fun main(args: Array<String>) {
//sampleStart
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
val filteredMap = originalMap.filter { it.value < 2 }
println(filteredMap) // {key1=1}
// original map has not changed
println(originalMap) // {key1=1, key2=2, key3=3}
val nonMatchingPredicate: ((Map.Entry<String, Int>)) -> Boolean = { it.value == 0 }
val emptyMap = originalMap.filter(nonMatchingPredicate)
println(emptyMap) // {}
//sampleEnd
}