Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 953
Abhishek Kumar Avatar asked Jul 23 '26 11:07

Abhishek Kumar


1 Answers

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

like image 143
Renato Avatar answered Jul 25 '26 03:07

Renato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!