Search code examples
javapriority-queue

Can I use Priority Queue in Java without implementing a comparator?


If I want to create priority queue of nodes, and each node has only one field (i.e. int val) do I have to write a comparator for the priority queue?


Solution

  • The elements of the priority queue are ordered according to their natural ordering, or by a Comparator . If you don't want to use Comparator then implement Comparable in your Node class.