I am new to java. I have not understood one thing while implementing the following code:
public class Demo
{
class DemoComparator implements Comparator<Board>
{
@Override
public int compare(Board A, Board B) {
return A.f()-B.f();
}
PriorityQueue<Board>Q = new PriorityQueue<>(10, new DemoComparator());
}
Here, Board is a class which I haven't shown and f() is a function that returns an integer value. My question is if I write:
Q.add(element1);
Q.add(element2);
Q.add(element3);
where, element1,element2 and element3 are of type Board and have their own f values returned from their respective f() function.
Will element1,element2 and element3 be stored in the priority queue according to their f values considering any increasing or decreasing order? and when the line: return A.f()-B.f(); will be called?
Comparable<T>instead ofComparator<T>?