Search code examples
flutterdartpriority-queue

How to implement priority Queue in dart?


Is Priority Queue collection available in dart because I am not able to use priority queue in flutter?

If so, then please write a snippet of how to use it. I am not able to find any handy explanation of how to use Priority Queue in flutter.


Solution

  • import 'package:collection/collection.dart';
    
    void main() {
      // queue that prioritizes longer strings
      final queue = PriorityQueue<String>((a, b) => b.length.compareTo(a.length));
      queue..add('foo')..add('bazars')..add('zort');
      
      while (queue.isNotEmpty) {
        print('* ${queue.removeFirst()}');
      }
    }
    

    Docs at https://pub.dev/documentation/collection/latest/collection/PriorityQueue-class.html