ZetCode

Dart Queue

last modified April 4, 2025

In Dart, Queue is a collection that implements a double-ended queue. It allows efficient addition and removal at both ends. Queue follows FIFO (First-In First-Out) principle by default.

Queue is part of dart:collection library. It provides more functionality than List for queue operations. Unlike List, Queue doesn't implement index-based access.

Creating a Queue

The simplest way to create a Queue is using the Queue constructor.

main.dart
import 'dart:collection';

void main() {
  var queue = Queue<String>();
  queue.add('Apple');
  queue.add('Banana');
  queue.add('Cherry');

  print(queue);
}

We create a Queue of Strings and add three elements. The add method appends elements to the end of the queue. The generic type specifies the element type.

$ dart main.dart
{Apple, Banana, Cherry}

Adding Elements to Queue

Queue provides multiple methods for adding elements at both ends.

main.dart
import 'dart:collection';

void main() {
  var queue = Queue<int>();
  
  // Add to end
  queue.add(1);
  queue.addLast(2);
  
  // Add to beginning
  queue.addFirst(0);
  
  // Add multiple
  queue.addAll([3, 4]);
  
  print(queue);
}

We demonstrate various addition methods. add and addLast append to the end, while addFirst prepends to the start. addAll adds multiple elements at once.

$ dart main.dart
{0, 1, 2, 3, 4}

Removing Elements from Queue

Queue provides methods to remove elements from both ends efficiently.

main.dart
import 'dart:collection';

void main() {
  var queue = Queue.from([10, 20, 30, 40, 50]);
  
  // Remove from start
  var first = queue.removeFirst();
  
  // Remove from end
  var last = queue.removeLast();
  
  // Remove specific element
  queue.remove(30);
  
  print('Removed: $first, $last');
  print('Remaining: $queue');
}

removeFirst and removeLast efficiently remove from ends. remove deletes the first occurrence of a value. All these methods modify the queue in-place.

$ dart main.dart
Removed: 10, 50
Remaining: {20, 40}

Queue Operations

Queue provides various operations for inspection and modification.

main.dart
import 'dart:collection';

void main() {
  var queue = Queue.from(['Red', 'Green', 'Blue']);
  
  // Check properties
  print('First: ${queue.first}');
  print('Last: ${queue.last}');
  print('Length: ${queue.length}');
  print('Is empty: ${queue.isEmpty}');
  
  // Clear queue
  queue.clear();
  print('After clear: $queue');
}

We demonstrate common Queue operations. first and last peek at elements without removing. length and isEmpty check size. clear removes all elements.

$ dart main.dart
First: Red
Last: Blue
Length: 3
Is empty: false
After clear: {}

Iterating Over Queue

Queue can be iterated using standard iteration methods.

main.dart
import 'dart:collection';

void main() {
  var queue = Queue.from([1, 2, 3, 4, 5]);
  
  // For-in loop
  print('For-in:');
  for (var num in queue) {
    print(num);
  }
  
  // forEach
  print('\nforEach:');
  queue.forEach(print);
  
  // Iterator
  print('\nIterator:');
  var it = queue.iterator;
  while (it.moveNext()) {
    print(it.current);
  }
}

We show three ways to iterate a Queue. The for-in loop is most common. forEach provides a functional approach. Iterator offers manual control.

$ dart main.dart
For-in:
1
2
3
4
5

forEach:
1
2
3
4
5

Iterator:
1
2
3
4
5

Best Practices

Source

Dart Queue Documentation

This tutorial covered Dart's Queue interface with practical examples demonstrating its key features and usage patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.