
- 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 Map - ContainsAll() Function
The Kotlin Map ContainsAll() function is used to checks whether all elements in the specified map are present in this map. If so, the function returns true. Otherwise, it returns false.
This function allows us to overcome the type-safety restriction of containsAll, which requires passing a collection of type Collection<E>.
In Kotlin, map is a representation of key-value pairs, so it is not directly a collection. This means we can not use method like containsAll() directly in map. Instead, the "entries" property of a map is used because entries represent a collection of key-value pairs.
Syntax
Following is the syntax of Kotlin Map ContainsAll() function −
fun containsAll(elements: mapOf<E>): Boolean
Parameters
This function accepts another map to search in this map as a parameter.
Return value
This function returns true if all elements of the input collection are present in this map and false otherwise.
Example 1: Checking Subset Relationships.
Let's see a basic example of the ContainsAll() function to verify if one map is a subset of another map −
fun main(args: Array<String>) { val mainMap = mapOf(1 to "aman", 2 to "tutorialspoint", 3 to "vivek", 4 to "Revathi") val subset = mapOf(1 to "aman", 2 to "tutorialspoint") print("Is subset available: ") println(mainMap.entries.containsAll(subset.entries)) }
Output
Following is the output −
Is subset available: true
Example 2: Filter Eligibility
The following example uses the ContainsAll() function to determine if an entity meets certain criteria −
fun main(args: Array<String>) { val availableSkills = mapOf("Java" to "Kotlin", "ReactJS" to "developer", "Pyhton" to "AI developer") val jobRequirements = mapOf("Java" to "Kotlin", "ReactJS" to "developer") println("Available Skills: ${availableSkills.entries}") println("Required Skills: ${jobRequirements.entries}") if (availableSkills.entries.containsAll(jobRequirements.entries)) { println("Eligible for the job!") } else { println("Not eligible.") } }
Output
Following is the output −
Available Skills: [Java=Kotlin, ReactJS=developer, Pyhton=AI developer] Required Skills: [Java=Kotlin, ReactJS=developer] Eligible for the job!