Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
listqueue
communitycreator

How to check the empty state of ListQueue in Dart

Programming Bytes

ListQueue is a list based on Queue.

Read more about ListQueue here.

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 object
  ListQueue queue = new ListQueue();

  print("queue is : $queue");
  // check if the queue is empty
  print("Is queue empty : ${queue.isEmpty}");

  // check if the not queue is empty
  print("Is queue not empty : ${queue.isNotEmpty}");

  // add two elements to the queue
  queue.add(10);
  queue.add(20);
  print("queue is : $queue");
  // check if the queue is empty
  print("Is queue empty : ${queue.isEmpty}");

  // check if the not queue is empty
  print("Is queue not empty : ${queue.isNotEmpty}");
}
Check the empty state of ListQueue in Dart

Explanation

  • Line 1: We import the collection library.

  • Line 4: We create a ListQueue named queue.

  • Line 8: We use the isEmpty property to check if the queue is empty. The queue has no elements in our case. Hence, true is returned.

  • Line 11: We use the isNotEmpty property to check if the queue is not empty. In our case, the queue has no elements. Hence, false is returned.

  • Lines 14-15: We use the add method to add two elements, 10 and 20.

  • Line 18: We use the isEmpty property to check if the queue is empty. In our case, the queue has two elements. Hence, false is returned.

  • Line 21: We use the isNotEmpty property to check if queue is not empty. In our case, the queue has two elements, so true is returned.

RELATED TAGS

dart
listqueue
communitycreator
RELATED COURSES

View all Courses

Keep Exploring