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 a 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()
{
// Creating a Queue
Queue<String> geek = new Queue<String>();
// Printing default value of queue
print(geek);
// Adding elements in a Queue
geek.add("Geeks");
geek.add("For");
geek.add("Geeks");
// Printing the inserted elements
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()
{
// Creating a List
List<String> geek_list = ["Geeks","For","Geeks"];
// Creating a Queue through a List
Queue<String> geek_queue = new Queue<String>.from(geek_list);
// Printing the elements
// in the queue
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.
Function Syntax | Description of the Function |
---|
queue_name.add(element) | Adds the element inside the queue from the back. |
queue_name.addAll(collection_name) | Adds all the element present in the collection_name (generally List). |
queue_name.addFirst(element) | Adds the element from front inside the queue. |
queue_name.addLast(element) | Adds the element from back in the queue. |
queue_name.clear() | Removes all the elements from the queue. |
queue_name.first() | Returns the first element from the queue. |
queue_name.forEach(f(element)) | Returns all the element present in the queue. |
queue_name.isEmpty | Returns boolean true if the queue is empty else return false. |
queue_name.length | Returns the length of the queue. |
queue_name.removeFirst() | Removes the first element from the queue. |
queue_name.removeLast() | Removes the last element from the queue. |
Example: Using various functions on Queue in Dart
Dart
import 'dart:collection';
void main()
{
// Creating a Queue
Queue<String> geek = new Queue<String>();
// Printing default value of queue
print(geek);
// Adding a element and displaying it
geek.add("Geeks");
print(geek);
// Adding multiple element and displaying it
List<String> geek_data = ["For","Geeks"];
geek.addAll(geek_data);
print(geek);
// Deleting all the data from queue
geek.clear();
print(geek);
// Checking if the queue is empty or not
print(geek.isEmpty);
// Adding first element
geek.addFirst("Geeks");
print(geek);
//Adding Last element
geek.addLast("For");
geek.addLast("Geeks");
print(geek);
// Checking length of the queue
print(geek.length);
// Removing First Element from queue
geek.removeFirst();
print(geek);
// Removing Last element from queue
geek.removeLast();
print(geek);
// Displaying all the elements
// of the queue
geek.forEach(print);
}
Output:
{}
{Geeks}
{Geeks, For, Geeks}
{}
true
{Geeks}
{Geeks, For, Geeks}
3
{For, Geeks}
{For}
For
Conclusion
In Dart, queues offer an effective way to manage data collections based on the First In, First Out (FIFO) principle. They are especially useful for handling ordered data that needs to be processed sequentially.
Queues can be created using constructors or initialized from existing lists. To avoid compilation errors, it is essential to import the `dart:collection` module. Dart also provides various functions for manipulating queues, such as adding, removing, and retrieving elements, which enhances the flexibility of data handling.
By understanding queue operations in Dart, developers can create optimized, structured, and efficient applications that require sequential data processing.
Explore
Dart Tutorial
7 min read
Basics
Data Types
Control Flow
Key Functions
Object-Oriented Programming
Dart Utilities
Dart Programs
Advance Concepts