Kotlin containsAll用法及代码示例

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

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

用法:

kotlin 复制代码
fun <T> Collection<T>.containsAll(
    elements: Collection<T>
): Boolean

检查指定集合中的所有元素是否都包含在此集合中。

允许克服需要传递类型为 Collection<E> 的集合的 containsAll 的 type-safety 限制。

代码示例:

kotlin 复制代码
import kotlin.test.*

fun main(args: Array<String>) {
    //sampleStart
    val collection = mutableListOf('a', 'b')
    val test = listOf('a', 'b', 'c')
    println("collection.containsAll(test) is ${collection.containsAll(test)}") // false

    collection.add('c')
    println("collection.containsAll(test) is ${collection.containsAll(test)}") // true
    //sampleEnd
}

// 输出
collection.containsAll(test) is false
collection.containsAll(test) is true

相关方法

相关推荐
曾经我也有梦想8 分钟前
Day4 Kotlin 高级特性
android
simplepeng33 分钟前
Compose Multiplatform 中的 Navigation 3
android
Kapaseker6 小时前
一杯美式讲完 Sealed Class
android·kotlin
冬奇Lab18 小时前
PowerManagerService(下):Doze模式与电池优化
android·源码阅读
砖厂小工19 小时前
Compose 中函数引用 vs Lambda:到底该用哪个?
android
Kapaseker1 天前
详解 Compose background 的重组陷阱
android·kotlin
黄林晴1 天前
Kotlin 2.3.20-RC2 来了!JPA 开发者狂喜,6 大更新一文速览
android·kotlin
冬奇Lab2 天前
OpenClaw 深度解析(八):Skill 系统——让 LLM 按需学习工作流
人工智能·开源·源码阅读
kymjs张涛2 天前
OpenClaw 学习小组:初识
android·linux·人工智能
糖猫猫cc2 天前
Kite:填充处理器
kotlin·orm·kite