Kotlin Set containsAll()
Syntax & Examples
Set.containsAll() function
The containsAll() function of the Set class in Kotlin checks if all elements in the specified collection are present in the set.
Syntax of Set.containsAll()
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.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
elements | required | The collection of elements to be checked for presence in the set. |
Return Type
Set.containsAll() returns value of type Boolean
.
✐ Examples
1 Using containsAll() to check for multiple elements in the set
In Kotlin, we can use the containsAll()
function to check if a set contains all elements of a specified collection.
For example,
- Create a set of integers.
- Create a collection of integers to check against the set.
- Use the
containsAll()
function to check if the set contains all elements of the collection. - Print the result to the console using the
println
function.
Kotlin Program
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")
}
Output
Does the set contain all elements in the list? true
2 Using containsAll() with an empty collection
In Kotlin, we can use the containsAll()
function to check if a set contains all elements of an empty collection.
For example,
- Create a set of strings.
- Create an empty collection of strings.
- Use the
containsAll()
function to check if the set contains all elements of the empty collection. - Print the result to the console using the
println
function.
Kotlin Program
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")
}
Output
Does the set contain all elements in the empty list? true
3 Using containsAll() with a mutable set
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,
- Create a mutable set of strings.
- Add elements to the set.
- Create a collection of strings to check against the set.
- Use the
containsAll()
function to check if the set contains all elements of the collection. - Print the result to the console using the
println
function.
Kotlin Program
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")
}
Output
Does the mutable set contain all elements in the list? true
Summary
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.