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

相关方法

相关推荐
lilian23320 分钟前
Harmony os 技术实战|拼豆制图06:收藏 ID、生成记录与重启恢复怎么不打架
android·java·数据库·harmonyos
2601_9557596227 分钟前
ClaudeAPI成本中心与业务标签设计指南
android·java·数据库
AFinalStone38 分钟前
Android 7系统异常问题排查(五)Framework层(下)—System Server崩溃
android·tombstone·系统异常
AFinalStone40 分钟前
Android 7系统异常问题排查(八)系统追踪—Trace机制与性能诊断
android·系统异常
极客猴子5 小时前
Android录音转写频繁卡顿?多款APP长时间会议场景稳定性实测
android·人工智能·智能手机·飞书
Kapaseker5 小时前
Boolean 变量到底该怎么命名?
android·kotlin
AFinalStone5 小时前
Android 7系统异常问题排查(六)应用异常(上)—ANR机制全解
android·系统异常
雾屿_Mistisle5 小时前
文件包含漏洞(File Inclusion)
android
刘名喜5 小时前
第21篇-N+1问题与fetch-join-EntityGraph
kotlin·springboot
小孔龙6 小时前
Android 图形系统全景
android·计算机图形学