I searched a bit and looks like a PriorityQueue
work like a min-heap by default in Java. So, why override the comparator? I have seen people do it even for integers. Am I missing something here? I am new to java.
I tried both of the below codes to use in a leetcode solution requiring min-heap
implementation and both got accepted. Here is a snippet:
// Implementing comparator
PriorityQueue<Integer> minHeap = new PriorityQueue<>(10,new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a - b;
}
});
// Not implementing comparator - why can't we just do this?
PriorityQueue<Integer> minHeap = new PriorityQueue<>();