Dart also provides the user to manipulate a collection of data in the form of a queue. A queue is a FIFO (First In First Out) data structure where the element that is added first will be deleted first. It takes the data from one end and removes it from the other end. Queues are useful when you want to build a first-in, first-out collection of data. It is the special case of list implementation of data in Dart.
Creating a Queue in Dart:
Using Constructor:
Queue variable_name = new Queue();
Through Existing List:
// With type notation(E)
Queue<E> variable_name = new Queue<E>.from(list_name);
// Without type notation
var variable_name = new Queue.from(list_name);
It must be noted that to use a queue in a dart program you have to import ‘dart:collection’ module. If you don’t do so then you will see the following error:
Error compiling to JavaScript:
main.dart:6:3:
Error: 'Queue; isn't a type
Queue<String> geek = new Queue<String>();
^^^^^
main.dart:6:28:
Error: Method not found: 'Queue'.
Queue<String> geek = new Queue<String>();
^^^^^
Error: Compilation failed.
Example 1: Creating a queue through a constructor and then inserting the elements in it.
Dart
import 'dart:collection' ;
void main()
{
Queue<String> geek = new Queue<String>();
print(geek);
geek.add( "Geeks" );
geek.add( "For" );
geek.add( "Geeks" );
print(geek);
}
|
Output:
{}
{Geeks, For, Geeks}
In the above code queue_name.add(element) is used to add the data in the queue.
Example 2: Creating a queue through a list.
Dart
import 'dart:collection' ;
void main()
{
List<String> geek_list = [ "Geeks" , "For" , "Geeks" ];
Queue<String> geek_queue = new Queue<String>.from(geek_list);
print(geek_queue);
}
|
Output:
{Geeks, For, Geeks}
Functions of Queue in Dart:
Dart also provides functions to manipulate queue created in the dart. Some important functions are listed in the table below followed by the example to use them.
Sr.No.
|
Function Syntax
|
Description of the Function
|
1.
|
queue_name.add(element) |
Adds the element inside the queue from the front. |
2.
|
queue_name.addAll(collection_name) |
Adds all the element present in the collection_name (generally List).
|
3.
|
queue_name.addFirst(element) |
Adds the element from front inside the queue. |
4.
|
queue_name.addLast(element) |
Adds the element from back in the queue. |
5.
|
queue_name.clear() |
Removes all the elements from the queue. |
6.
|
queue_name.first() |
Returns the first element from the queue. |
7.
|
queue_name.forEach(f(element)) |
Returns all the element present in the queue. |
8.
|
queue_name.isEmpty |
Returns boolean true if the queue is empty else return false. |
9.
|
queue_name.length |
Returns the length of the queue. |
10.
|
queue_name.removeFirst() |
Removes the first element from the queue. |
11.
|
queue_name.removeLast() |
Removes the last element from the queue. |
Example: Using various functions on Queue in Dart.
Dart
import 'dart:collection' ;
void main()
{
Queue<String> geek = new Queue<String>();
print(geek);
geek.add( "Geeks" );
print(geek);
List<String> geek_data = [ "For" , "Geeks" ];
geek.addAll(geek_data);
print(geek);
geek.clear();
print(geek);
print(geek.isEmpty);
geek.addFirst( "Geeks" );
print(geek);
geek.addLast( "For" );
geek.addLast( "Geeks" );
print(geek);
print(geek.length);
geek.removeFirst();
print(geek);
geek.removeLast();
print(geek);
geek.forEach(print);
}
|
Output:
{}
{Geeks}
{Geeks, For, Geeks}
{}
true
{Geeks}
{Geeks, For, Geeks}
3
{For, Geeks}
{For}
For