
- 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 - isEmpty() Function
The Kotlin Set isEmpty() function is used to return the boolean value. True if the set is empty means the set does not contain any element. False if the set contains the element.
There are the following use cases of the isEmpty() function:
- Validation: Before proceeding with a set, make sure it contains elements to avoid errors or unnecessary operations.
- Conditional Logic: This function executes different code paths depending on whether the set is empty or not.
The isEmpty() function enhances readability, reduces boilerplate code, and improves code safety in various scenarios.
Syntax
Following is the syntax of Kotlin set isEmpty() function −
Set.isEmpty()
Parameters
This function does not accepts any parameters.
Return value
This function returns true if the set does not contains any element otherwise returns false.
Example 1: Checking if a Set is Empty or NOT
Let's see a basic example of the isEmpty() function, which returns true if the set is empty.
fun main(args: Array<String>) { val emptySet = setOf<String>() if (emptySet.isEmpty()) { println("The set is empty.") } else { println("The set is not empty.") } }
Output
Following is the output −
The set is empty.
Example 2: Return Boolean Value
This is another example of the isEmpty() function, which returns a boolean value indicating whether the set is empty; if not, it returns false −
fun main(args: Array<String>) { val emptySet = setOf<String>() val populatedSet = setOf("Kotlin", "Java", "Python") println("Is set empty? ${emptySet.isEmpty()}") println("Is set empty? ${populatedSet.isEmpty()}") }
Output
Following is the output −
Is set empty? true Is set empty? false
Example 3: Validating User Permission
This example uses the isEmpty() function to validate the user's permissions if the set is empty: The user has been granted no permissions. Otherwise, display details of the user's permissions −
fun main(args: Array<String>) { val userPermissions: Set<String> = setOf() if (userPermissions.isEmpty()) { println("No permissions assigned to the user.") } else { println("User has the following permissions: $userPermissions") } }
Output
Following is the output −
No permissions assigned to the user.