How to check the empty state of ListQueue in Dart
ListQueue is a list based on Queue.
Read more about
ListQueuehere.
The isEmpty property checks if no elements are present in ListQueue.
The isNotEmpty property checks if at least one element is present in the ListQueue.
Syntax
queue.isEmpty
queue.isNotEmpty
Return value
The isEmpty property returns true if no element is present in the queue. Otherwise, it returns false.
The isNotEmpty property returns true if the queue has at least one element. Otherwise, it returns false.
Code
The code below demonstrates how to check the empty state of the queue.
import 'dart:collection';void main() {// create a new ListQueue objectListQueue queue = new ListQueue();print("queue is : $queue");// check if the queue is emptyprint("Is queue empty : ${queue.isEmpty}");// check if the not queue is emptyprint("Is queue not empty : ${queue.isNotEmpty}");// add two elements to the queuequeue.add(10);queue.add(20);print("queue is : $queue");// check if the queue is emptyprint("Is queue empty : ${queue.isEmpty}");// check if the not queue is emptyprint("Is queue not empty : ${queue.isNotEmpty}");}
Explanation
-
Line 1: We import the
collectionlibrary. -
Line 4: We create a
ListQueuenamedqueue. -
Line 8: We use the
isEmptyproperty to check if thequeueis empty. The queue has no elements in our case. Hence,trueis returned. -
Line 11: We use the
isNotEmptyproperty to check if thequeueis not empty. In our case, the queue has no elements. Hence,falseis returned. -
Lines 14-15: We use the
addmethod to add two elements, 10 and 20. -
Line 18: We use the
isEmptyproperty to check if thequeueis empty. In our case, the queue has two elements. Hence,falseis returned. -
Line 21: We use the
isNotEmptyproperty to check ifqueueis not empty. In our case, the queue has two elements, sotrueis returned.