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

ParameterOptional/RequiredDescription
elementsrequiredThe 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,

  1. Create a set of integers.
  2. Create a collection of integers to check against the set.
  3. Use the containsAll() function to check if the set contains all elements of the collection.
  4. 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,

  1. Create a set of strings.
  2. Create an empty collection of strings.
  3. Use the containsAll() function to check if the set contains all elements of the empty collection.
  4. 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,

  1. Create a mutable set of strings.
  2. Add elements to the set.
  3. Create a collection of strings to check against the set.
  4. Use the containsAll() function to check if the set contains all elements of the collection.
  5. 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.