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}
{}

相关方法

相关推荐
冬奇Lab6 小时前
OpenClaw 源码深度解析(一):Gateway——为什么需要一个"中枢"
人工智能·开源·源码阅读
不爱说话郭德纲9 小时前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
Sinclair13 小时前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
A0微声z15 小时前
Kotlin Multiplatform (KMP) 中使用 Protobuf
kotlin
雮尘16 小时前
手把手带你玩转Android gRPC:一篇搞定原理、配置与客户端开发
android·前端·grpc
ktl18 小时前
Android 编译加速/优化 80%:一个文件搞定,零侵入零配置
android
alexhilton1 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
冬奇Lab1 天前
InputManagerService:输入事件分发与ANR机制
android·源码阅读
张小潇1 天前
AOSP15 Input专题InputManager源码分析
android·操作系统
lhDream1 天前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin