To check if a Set contains all the items of another Set in Kotlin, you can use the containsAll
method.
In this example,
set1
and set2
containing elements of a specific type.containsAll
method of set1
to check if it contains all the items of set2
.containsAll
method returns true
if set1
contains all the items of set2
, otherwise it returns false
.fun main() {
// Define Sets with items
val set1 = setOf("apple", "banana", "orange", "grape", "melon")
val set2 = setOf("banana", "orange")
// Check if set1 contains all items of set2
val containsAllItems = set1.containsAll(set2)
// Print result
println("Does set1 contain all items of set2? $containsAllItems")
}
Does set1 contain all items of set2? true
In this example,
set1
and set2
containing elements of a specific type.containsAll
method of set1
to check if it contains all the items of set2
.containsAll
method returns false
if set1
does not contain all the items of set2
.fun main() {
// Define Sets with items
val set1 = setOf("apple", "banana", "orange", "grape", "melon")
val set2 = setOf("banana", "peach")
// Check if set1 contains all items of set2
val containsAllItems = set1.containsAll(set2)
// Print result
println("Does set1 contain all items of set2? $containsAllItems")
}
Does set1 contain all items of set2? false
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Kotlin language with well detailed examples.