如果我将它与 PriorityQueue 和类似接口一起使用,poll() 方法 returns 有什么作用
What does poll() method returns if i use it with PriorityQueue and comparable interface
我正在使用 PriorityQueue
并且我已经使用 compareTo 方法实现了可比较的 class,
现在我想知道我的队列是否已排序,如果我使用 poll()
方法,这个 return 最小 costSum 的队列会吗?
Class: State.java
public class State<N extends Comparable<N>> implements Comparable<State<N>> {
private final ArrayList<Integer> board;
private State<N> predecessor;
private double totalCostFromStart; //g(x)
private double minimumRemainingCostToTarget; //h(x)
private double costSum; //f(x)
private Move direction;
public State(ArrayList<Integer> board,
State<N> predecessor,
double minimumRemainingCostToTarget,
Move direction) {
this.board = board;
this.predecessor = predecessor;
this.totalCostFromStart = predecessor == null ? 0 : predecessor.totalCostFromStart + 1;
this.minimumRemainingCostToTarget = minimumRemainingCostToTarget;
this.direction=direction;
calculateCostSum();
}
private void calculateCostSum() {
this.costSum = this.totalCostFromStart + this.minimumRemainingCostToTarget;
}
@Override
public int compareTo(State<N> nNode) {
int compare = Double.compare(this.costSum, nNode.costSum);
if (compare == 0) return 0;
else return this.costSum>nNode.costSum ? 1:-1;
}
Class : AStar.java
public State AStar(ArrayList<Integer> initialBoard,
State source,
ArrayList<Integer> target,
Heuristic heuristic){
int minimumRemainingCostToTarget= heuristic.getRank(initialBoard, target);
source = new State( initialBoard,null,0, minimumRemainingCostToTarget,null);
PriorityQueue<State> open = new PriorityQueue<>();
Set<ArrayList<Integer>> close = new HashSet<>(181440);
//add initial state to ouverts, f(n) is an attribut in source.
open.add(source);
while(!close.isEmpty()){
State currentState = open.poll();//<<<----------------------
}
return null;
}
Now i want to know if my queue is sorted, if i use poll() method will this return the queue of the minimum costSum?
Javadoc是这样描述的:
The head of this queue is the least element with respect to the specified ordering. ... The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
所以,是的,它是最小元素。
但是请注意,队列不是内部排序:如果您打印优先级队列,您可能会注意到它们没有按升序出现。这些元素只是简单地按照堆 属性 的顺序存储,一旦最小元素被删除,就可以有效地更新数据结构。
我正在使用 PriorityQueue
并且我已经使用 compareTo 方法实现了可比较的 class,
现在我想知道我的队列是否已排序,如果我使用 poll()
方法,这个 return 最小 costSum 的队列会吗?
Class: State.java
public class State<N extends Comparable<N>> implements Comparable<State<N>> {
private final ArrayList<Integer> board;
private State<N> predecessor;
private double totalCostFromStart; //g(x)
private double minimumRemainingCostToTarget; //h(x)
private double costSum; //f(x)
private Move direction;
public State(ArrayList<Integer> board,
State<N> predecessor,
double minimumRemainingCostToTarget,
Move direction) {
this.board = board;
this.predecessor = predecessor;
this.totalCostFromStart = predecessor == null ? 0 : predecessor.totalCostFromStart + 1;
this.minimumRemainingCostToTarget = minimumRemainingCostToTarget;
this.direction=direction;
calculateCostSum();
}
private void calculateCostSum() {
this.costSum = this.totalCostFromStart + this.minimumRemainingCostToTarget;
}
@Override
public int compareTo(State<N> nNode) {
int compare = Double.compare(this.costSum, nNode.costSum);
if (compare == 0) return 0;
else return this.costSum>nNode.costSum ? 1:-1;
}
Class : AStar.java
public State AStar(ArrayList<Integer> initialBoard,
State source,
ArrayList<Integer> target,
Heuristic heuristic){
int minimumRemainingCostToTarget= heuristic.getRank(initialBoard, target);
source = new State( initialBoard,null,0, minimumRemainingCostToTarget,null);
PriorityQueue<State> open = new PriorityQueue<>();
Set<ArrayList<Integer>> close = new HashSet<>(181440);
//add initial state to ouverts, f(n) is an attribut in source.
open.add(source);
while(!close.isEmpty()){
State currentState = open.poll();//<<<----------------------
}
return null;
}
Now i want to know if my queue is sorted, if i use poll() method will this return the queue of the minimum costSum?
Javadoc是这样描述的:
The head of this queue is the least element with respect to the specified ordering. ... The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
所以,是的,它是最小元素。
但是请注意,队列不是内部排序:如果您打印优先级队列,您可能会注意到它们没有按升序出现。这些元素只是简单地按照堆 属性 的顺序存储,一旦最小元素被删除,就可以有效地更新数据结构。