Kotlin filterNot用法及代码示例

本文方法及代码示例基于Kotlin 2.1.20 Released

filterNot 所在包 kotlin.collections.filterNot,其相关用法介绍如下:

用法一

kotlin 复制代码
inline fun <T> Array<out T>.filterNot(
    predicate: (T) -> Boolean
): List<T>

inline fun ByteArray.filterNot(
    predicate: (Byte) -> Boolean
): List<Byte>

inline fun ShortArray.filterNot(
    predicate: (Short) -> Boolean
): List<Short>

inline fun IntArray.filterNot(
    predicate: (Int) -> Boolean
): List<Int>

inline fun LongArray.filterNot(
    predicate: (Long) -> Boolean
): List<Long>

inline fun FloatArray.filterNot(
    predicate: (Float) -> Boolean
): List<Float>

inline fun DoubleArray.filterNot(
    predicate: (Double) -> Boolean
): List<Double>

inline fun BooleanArray.filterNot(
    predicate: (Boolean) -> Boolean
): List<Boolean>

inline fun CharArray.filterNot(
    predicate: (Char) -> Boolean
): List<Char>

inline fun <T> Iterable<T>.filterNot(
    predicate: (T) -> Boolean
): List<T>

@ExperimentalUnsignedTypes inline fun UIntArray.filterNot(
    predicate: (UInt) -> Boolean
): List<UInt>

@ExperimentalUnsignedTypes inline fun ULongArray.filterNot(
    predicate: (ULong) -> Boolean
): List<ULong>

@ExperimentalUnsignedTypes inline fun UByteArray.filterNot(
    predicate: (UByte) -> Boolean
): List<UByte>

@ExperimentalUnsignedTypes inline fun UShortArray.filterNot(
    predicate: (UShort) -> Boolean
): List<UShort>

返回一个列表,其中包含与给定 predicate 不匹配的所有元素。

示例代码:

kotlin 复制代码
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
}

// 输出 
[2, 4, 6]
[1, 2, 4, 5, 7]

用法二

kotlin 复制代码
inline fun <K, V> Map<out K, V>.filterNot(
    predicate: (Entry<K, V>) -> Boolean
): Map<K, V>

返回一个包含与给定 predicate 不匹配的所有键值对的新映射。

返回的映射保留原始映射的条目迭代顺序。

示例代码:

kotlin 复制代码
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.filterNot { it.value < 3 }
    println(filteredMap) // {key3=3}
    // original map has not changed
    println(originalMap) // {key1=1, key2=2, key3=3}

    val matchAllPredicate: ((Map.Entry<String, Int>)) -> Boolean = { it.value > 0 }
    val emptyMap = originalMap.filterNot(matchAllPredicate)
    println(emptyMap) // {}
    //sampleEnd
}

// 输出 
{key3=3}
{key1=1, key2=2, key3=3}
{}

相关方法

相关推荐
移动开发者1号6 分钟前
解析 Android Doze 模式与唤醒对齐
android·kotlin
Devil枫2 小时前
Kotlin扩展函数与属性
开发语言·python·kotlin
菠萝加点糖2 小时前
Kotlin Data包含ByteArray类型
android·开发语言·kotlin
IAM四十二9 天前
Google 端侧 AI 框架 LiteRT 初探
android·深度学习·tensorflow
CYRUS_STUDIO9 天前
手把手教你用 Chrome 断点调试 Frida 脚本,JS 调试不再是黑盒
android·app·逆向
Just丶Single10 天前
安卓NDK初识
android
编程乐学10 天前
网络资源模板--基于Android Studio 实现的咖啡点餐App
android·android studio·大作业·奶茶点餐·安卓移动开发·咖啡点餐
二流小码农10 天前
鸿蒙开发:基于node脚本实现组件化运行
android·ios·harmonyos
Wgllss10 天前
Kotlin+协程+FLow+Channel+Compose 实现一个直播多个弹幕效果
android·架构·android jetpack
续天续地10 天前
开箱即用的Kotlin Multiplatform 跨平台开发模板:覆盖网络/存储/UI/DI/CI工具链
ios·kotlin