The containsAll() function of the Set class in Kotlin checks if all elements in the specified collection are present in the set.
The syntax of Set.containsAll() function is:
abstract fun containsAll(elements: Collection<E>): Boolean
This containsAll() function of Set checks if all elements in the specified collection are contained in this set.
Parameter | Optional/Required | Description |
---|---|---|
elements | required | The collection of elements to be checked for presence in the set. |
Set.containsAll() returns value of type Boolean
.
In Kotlin, we can use the containsAll()
function to check if a set contains all elements of a specified collection.
For example,
containsAll()
function to check if the set contains all elements of the collection.println
function.fun main(args: Array<String>) {
val numbers = setOf(1, 2, 3, 4, 5)
val checkList = listOf(2, 4)
val containsAll = numbers.containsAll(checkList)
println("Does the set contain all elements in the list? $containsAll")
}
Does the set contain all elements in the list? true
In Kotlin, we can use the containsAll()
function to check if a set contains all elements of an empty collection.
For example,
containsAll()
function to check if the set contains all elements of the empty collection.println
function.fun main(args: Array<String>) {
val fruits = setOf("apple", "banana", "cherry")
val emptyList = listOf<String>()
val containsAll = fruits.containsAll(emptyList)
println("Does the set contain all elements in the empty list? $containsAll")
}
Does the set contain all elements in the empty list? true
In Kotlin, the containsAll()
function can also be used with mutable sets to check for the presence of all elements in a specified collection.
For example,
containsAll()
function to check if the set contains all elements of the collection.println
function.fun main(args: Array<String>) {
val mutableSet = mutableSetOf("apple", "banana", "cherry")
mutableSet.add("date")
val checkList = listOf("banana", "date")
val containsAll = mutableSet.containsAll(checkList)
println("Does the mutable set contain all elements in the list? $containsAll")
}
Does the mutable set contain all elements in the list? true
In this Kotlin tutorial, we learned about containsAll() function of Set: the syntax and few working examples with output and detailed explanation for each example.