Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is DoubleLinkedQueue.clear() method in Dart?

Surabhi Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The dart:collection library provides advanced collection support for the Dart language. It also contains the DoubleLinkedQueue<E> class which implements the abstract Queue<E> class using a doubly-linked list.

The clear() method

The DoubleLinkedQueue<E> class contains the clear() method which removes all the elements of the queue. The queue size becomes zero after calling the clear() method.

A queue is a FIFO (First In First Out) data structure. In a queue, the element that is added first will be deleted first.

The clear() method modifies the state of the queue. The illustration below shows how the clear() method works.

Removing all elements of queue using clear() method

Syntax

The prototype of the clear() method is shown below.

void clear()

Arguments

The clear() method does not take any arguments.

Return values

  • The clear() method does not return any value.
  • The clear() method is a constant-time operation.

Code

The code below shows how the clear() method can be used in Dart.

import 'dart:collection';
void main() {
var quadNations = DoubleLinkedQueue<String>();
quadNations.add("India");
quadNations.add("US");
quadNations.add("Australia");
quadNations.add("Japan");
print('Printing Queue Elements');
print(quadNations);
quadNations.clear();
print('Removed all elementes using clear() method');
print('Printing Queue Elements');
print(quadNations);
}

Explanation

The code above performs the following actions:

  • An instance of the DoubleLinkedQueue class of type String is initialized in line 3.

  • Next, a few strings, "India", "US", "Australia", and "Japan", are added to the queue using the add() method.

  • The queue elements are displayed as {India, US, Australia, Japan}. All the elements are added to the end of the queue, as the queue is a FIFO data structure.

  • The clear() method in line 12 removes all elements of the queue.

  • Upon printing all the queue elements again, it can be observed that the queue has become empty and there are no elements in the queue.

RELATED TAGS

dart
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring