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

相关方法

相关推荐
BoomHe1 天前
Android AOSP13 原生 Launcher3 壁纸获取方式
android
Digitally1 天前
如何将联系人从 Android 转移到 Android
android
一直在想名1 天前
Flutter 框架跨平台鸿蒙开发 - 黑白屏
flutter·华为·kotlin·harmonyos
李小枫1 天前
webflux接收application/x-www-form-urlencoded参数
android·java·开发语言
爱丽_1 天前
MySQL `EXPLAIN`:看懂执行计划、判断索引是否生效与排错套路
android·数据库·mysql
NPE~1 天前
[App逆向]环境搭建下篇 — — 逆向源码+hook实战
android·javascript·python·教程·逆向·hook·逆向分析
yewq-cn1 天前
AOSP 下载
android
cch89181 天前
Laravel vs ThinkPHP:PHP框架终极对决
android·php·laravel
米码收割机1 天前
【Android】基于安卓app的汽车租赁管理系统(源码+部署方式+论文)[独一无二]
android·汽车
流星雨在线1 天前
安卓使用 Startup 管理三方 SDK 初始化
android·startup