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

相关方法

相关推荐
程序员陆业聪5 小时前
绕过Frida/Xposed的最后防线:SVC直接系统调用与Native反Hook实战
android
程序员陆业聪5 小时前
WebView与原生JS交互:JSBridge生产级实现与安全防护
android
我命由我123458 小时前
Android 开发问题:MlKitException: An internal error occurred during initialization.
android·java·java-ee·android jetpack·android-studio·androidx·android runtime
Meteors.8 小时前
Android自定义 View 三核心方法详解
android
2501_916007478 小时前
前端开发常用软件与工具全面指南
android·ios·小程序·https·uni-app·iphone·webview
赏金术士9 小时前
Android Tinker 热修复集成与使用指南 1.9.15.2
android·热修复·tinker
Refrain_zc10 小时前
Android 音视频通话核心 —— 音频解码(AAC → PCM → 播放)完整解析
kotlin
2603_9541383910 小时前
安卓误删文件先别慌!5个实用小技巧指南教你补救
android·智能手机
Refrain_zc10 小时前
Android 音视频通话核心 —— Camera 采集 + 音视频编码调度
kotlin
波诺波12 小时前
5-SOFA可变形的3D物体 5-elasticity.scn
android