
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Set - ContainsAll() Function
The Kotlin set ContainsAll() function is used to checks whether all elements in the specified set are present in this set. If so, the function returns true. Otherwise, it returns false.
This function allows overcoming type-safety restriction of containsAll that requires passing a collection of type Collection<E>.
Syntax
Following is the syntax of Kotlin Set ContainsAll() function −
fun containsAll(elements: setOf<E>): Boolean
Parameters
This function accepts another set to search in this set as a parameter.
Return value
This function returns true if all elements of the input collection are present in this set and false otherwise.
Example 1: Checking All Element Presence in Set.
Let's see a basic example of the ContainsAll() function to verify if collection exists in mutableSet.
fun main(args: Array<String>) { val collection = mutableSetOf('a', 'b') val test = setOf('a', 'b', 'c') println("collection.containsAll(test) is ${collection.containsAll(test)}") collection.add('c') println("collection.containsAll(test) is ${collection.containsAll(test)}") }
Output
Following is the output −
collection.containsAll(test) is false collection.containsAll(test) is true
Example 2: Checking Permission
The following example uses the ContainsAll() function to verify if a user has all the required permissions −
fun main(args: Array<String>) { val userPermissions = setOf("READ", "WRITE", "EXECUTE") val requiredPermissions = setOf("READ", "WRITE") if (userPermissions.containsAll(requiredPermissions)) { println("Access granted.") } else { println("Access denied.") } }
Output
Following is the output −
Access granted.
Example 3: Input Validation
This is another example of ContainsAll() function. Here, we may need to validate that user input contains specific required elements −
fun main(args: Array<String>) { val requiredFields = setOf("name", "email", "password") val userInputFields = setOf("name", "email", "password", "age") if (userInputFields.containsAll(requiredFields)) { println("Valid input!") } else { println("Missing required fields.") } }
Output
Following is the output −
Valid input!