Basically I have a priorityqueue that has a custom comparator which takes into account values that might be changed without touching the queue itself. So I want the queue to recheck itself and rearange the elements.How would I go about doing that?
a example of what I mean:
public NewClass() {
PriorityQueue pq = new PriorityQueue(11,new CompareInts());
CompareInts o1 = new CompareInts();
CompareInts o2 = new CompareInts();
o2.state = 1;
pq.add(o1);
pq.add(o2);
o1.state = 3;
//if o1 was added to the queue here, it'd be removed first
System.out.println(pq.remove());//outputs 1
System.out.println(pq.remove());//outputs 3
}
class CompareInts implements Comparator<CompareInts>
{
public int state;
public CompareInts() {
state=0;
}
@Override
public int compare(CompareInts o1, CompareInts o2) {
return o2.state-o1.state;
}
@Override
public String toString() {
return String.valueOf(state);
}
}
EDIT: by the way, I understand adding an empty object and removing it would do the trick, but I'd prefer it be as optimal as possible