
- 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 List - isNotEmpty() Function
The Kotlin List isNotEmpty() function is used to return the boolean value. True if the list is not empty means the list contains elements. False if the list does not contains any element.
There are the following use cases of the isNotEmpty() function:
- Validation: It ensure that the list contains element before proceeding with operation that required non-empty list.
- Conditional Logic: Execute different action based on whether a collection is empty or not.
- User Notification: If the list 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 list isNotEmpty() function −
List.isNotEmpty()
Parameters
This function does not accepts any parameters.
Return value
This function returns true if the list contains elements otherwise returns false.
Example 1: Checking Whether List is Empty or not
Let's see a basic example of the isNotEmpty() function, which returns true if the list is not empty.
fun main(args: Array<String>) { val list = listOf(1, 2, 3); val isNotEmpty = list.isNotEmpty(); if(isNotEmpty == true){ println("list is not empty!") println(list) }else{ println("list is empty!") } }
Output
Following is the output −
list is not empty! [1, 2, 3]
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 = listOf(10, 20, 30, 40) if (tasks.isNotEmpty()|| tasks.size>2) { println("Task is not empty") println("Tasks are ready for processing.") println("${tasks.size}") } else { println("Not enough tasks to proceed.") } }
Output
Following is the output −
Task is not empty Tasks are ready for processing. 4
Example 3: If the List is Empty
This is another example of isNotEmpty() function if the list is empty, it return the false −
fun main(args: Array<String>) { val numbers = listOf<String>() val flag = false if (numbers.isNotEmpty()) { println("Is list is not empty? " + flag) } else { println("Is list empty? " + !flag) } }
Output
Following is the output −
Is list empty? true