The kotlin.collections
package is part of Kotlin’s standard library.
It contains all collection types, such as:
Map
List
Set
, etc.The package provides the ArrayList
class, which is a mutable list and uses a dynamic resizable array as the backing storage.
isEmpty()
methodThe ArrayList
class contains the isEmpty()
method, which is used to check if the array list is empty or not.
If the list does not have any elements, it returns true
. Otherwise, it returns false
.
fun isEmpty(): Boolean
true
if the list is empty, and false
otherwise.ArrayList
follows the sequence of insertion of elements.
It is allowed to contain duplicate elements.
The illustration below shows the function of the isEmpty()
method.
The ArrayList
class is present in the kotlin.collection
package and is imported by default in every Kotlin program.
fun main(args : Array<String>) {val companyList = ArrayList<String>()companyList.add("Apple")companyList.add("Google")companyList.add("Microsoft")val emptyList = ArrayList<String>()println("Printing companyList elements--")println(companyList)println("Printing emptyList elements--")println(emptyList)println("Checking if companyList is empty using isEmpty() : ${companyList.isEmpty()}")println("Checking if emptyList is empty using isEmpty() : ${emptyList.isEmpty()}")}
First, we create an empty array list named companyList
to store the strings.
Next, we use the add()
method to add a few company names to the ArrayList
object, such as: "Google"
, "Apple"
, "Microsoft"
, and "Netflix"
.
We create another empty array list, emptyList
.
Next, we call the isEmpty()
method on the companyList
and it returns false
, as it has few elements.
We again call the isEmpty()
method on the emptyList
and it returns true
.
The array list elements and result of isEmpty()
method are displayed using the print()
function of the kotlin.io
package.