
- 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 - isNotEmpty() Function
The Kotlin Map isNotEmpty() function is used to return the boolean value. True if the map is not empty means the map contains elements. False if the map does not contains any element.
There are the following use cases of the isNotEmpty() function:
- Validation: It ensure that the map contains element before proceeding with operation that required non-empty map.
- Conditional Logic: Execute different action based on whether a collection is empty or not.
- User Notification: If the map is empty, it displays a message to users based on the availability of items, such as "No items found."
Syntax
Following is the syntax of Kotlin map isNotEmpty() function −
Map.isNotEmpty()
Parameters
This function does not accepts any parameters.
Return value
This function returns true if the map contains elements otherwise returns false.
Example 1: Checking Whether Map is Empty or not
Let's see a basic example of the isNotEmpty() function, which returns true if the map is not empty.
fun main(args: Array<String>) { val map = mapOf(1 to "aman", 2 to "akash", 3 to "Rahul"); val isnotempty = map.isNotEmpty(); if(isnotempty == true){ println("map is not empty!") println(map) }else{ println("map is empty!") } }
Output
Following is the output −
map is not empty! {1=aman, 2=akash, 3=Rahul}
Example 2: Short-Circuit Logic
The following example combines isNotEmpty() with the other checks to simplify the logic −
fun main(args: Array<String>) { val tasks = mapOf(1 to 'A', 5 to 'E', 10 to 'J') if (tasks.isNotEmpty()|| tasks.size>2) { println("map is not empty: " + tasks) println("Tasks are ready for processing.") println("${tasks.size}") } else { println("Not enough tasks to proceed.") } }
Output
Following is the output −
map is not empty: {1=A, 5=E, 10=J} Tasks are ready for processing. 3
Example 3: If the Map is Empty
This is another example of isNotEmpty() function if the map is empty, it return the false −
fun main(args: Array<String>) { val map = mapOf<Int, String>() val res = map.isNotEmpty(); println("Is map not empty? " + res) }
Output
Following is the output −
Is map not empty? false