'Efficient way on sorting priority queue of objects by specified value
I want the priority queue auto to sort its elements in ascending way by objects' value. "Entry" is one of my custom class. Currently I'm doing it in this way:
static Comparator<Entry> comparator = new MindistComparator();
static PriorityQueue<Entry> listH = new PriorityQueue<Entry>(10, comparator);
public class MindistComparator implements Comparator<Rtree.Entry> {
@Override
public int compare(Rtree.Entry o1, Rtree.Entry o2) {
if(o1.getMindist() < o2.getMindist()){
return -1;
}
if(o1.getMindist() > o2.getMindist()){
return 1;
}
return 0;
}
}
Custom class: entry
public static class Entry{
private Node node;
private Double mindist;
public Entry(Node node, Double mindist){
this.node = node;
this.setMindist(mindist);
}
public void setMindist(Double mindist) {
this.mindist = mindist;
}
public Double getMindist() {
return mindist;
}
}
I measure the execution time by running:
listH.add(new Entry(n,mindist));
several times (let's say 1000.).
I can get the correct output through above code. But this process still takes longer time than I expected. I'm looking for a more efficient way to sort the priority queue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
