The dart:collection
library provides advanced collection support for the Dart language. The library contains the DoubleLinkedQueue<E>
class, which uses a doubly-linked list to implement the abstract Queue<E>
class.
The DoubleLinkedQueue<E>
class contains the addAll()
method which is used to add the elements of a specified input collection to the end of the queue. The queue’s length is extended by the input collection’s length.
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 figure below illustrates how the addAll()
method works:
The syntax of the addAll()
method is as follows:
void addAll(Iterable<E> iterable)
The addAll()
method takes a collection of elements as input and adds all elements to the end of the queue.
addAll()
does not return any value.
The code below shows how the addAll()
method works in Dart:
import 'dart:collection';void main() {var monthQueue = DoubleLinkedQueue<String>();monthQueue.add("January");monthQueue.add("February");monthQueue.add("March");print('Queue : $monthQueue');var otherList = ["April","May"];print('List : $otherList');monthQueue.addAll(otherList);print('Queue after addAll() : $monthQueue');}
Line 3: We create an instance named monthQueue
of DoubleLinkedQueue
class of type String
.
Lines 5-7: Next, we use the add()
method to add a few strings to the queue: "January"
, "February"
, and "March"
.
Line 9: We create a list of strings and add "April"
and "May"
to it.
Line 12: We now call the addAll()
method and pass the list created above as input. It adds all list elements to the end of the queue, as the queue is a FIFO data structure.
Line 14: We use the print()
function of the core library to display the queue and list elements.