Dart: Collection Queue

Identifier = new Queue()

Example

import 'dart:collection'; 
void main() { 
   Queue queue = new Queue(); 
   print("Default implementation ${queue.runtimeType}"); 
   queue.add(10); 
   queue.add(20); 
   queue.add(30); 
   
   for(var no in queue){ 
      print(no); 
   } 
}      

=> output

Default implementation ListQueue
10 
20 
30 

# Adding Multiple Values to a Queue

addAll() function enables adding multiple values to a queue

import 'dart:collection'; 
void main() { 
   Queue queue = new Queue(); 
   print("Default implementation ${queue.runtimeType}"); 
   queue.addAll([10,20,30]); 
   for(var no in queue){ 
      print(no); 
   } 
}

It should produce the following output −

Default implementation ListQueue 
10 
20
30 

# Adding Value at the Beginning and End of a Queue

addFirst(): the method adds the specified value to the beginning of the queue.

import 'dart:collection'; 
void main() { 
   Queue numQ = new Queue(); 
   numQ.addAll([10,20]); 
   print("Printing Q.. ${numQ}");
   numQ.addFirst(30); 
   print("Printing Q.. ${numQ}"); 
}  

=> output

Printing Q.. {10, 20} 
Printing Q.. {30, 10, 20}

addLast(): the function adds the specified object to the end of the queue.

import 'dart:collection'; 
void main() { 
   Queue numQ = new Queue(); 
   numQ.addAll([10,20]); 
   print("Printing Q.. ${numQ}");  
   numQ.addLast(30); 
   print("Printing Q.. ${numQ}"); 
} 

It should produce the following output −

Printing Q.. {10, 20} 
Printing Q.. {10, 20, 30} 

Leave a Reply

Your email address will not be published. Required fields are marked *