这个PriorityQueue有什么问题?

Ton*_*ony 0 java

fringe = new PriorityQueue<Node>(10,new Comparator<Node>(){
            @Override
            public int compare(Node node1,Node node2)
            {
                if (f(node1)>f(node2))
                    return 1;
                else
                    return -1;
            }
        });
Run Code Online (Sandbox Code Playgroud)

我声明了一个PQ来存储一些节点,我想根据f值以非递减顺序存储节点.函数f(节点节点)是计算节点的f值.所以我覆盖了比较器,但是现在我发现一些f值较大的节点放在队列中f值较小的节点之前,我检查了一遍,但仍然找不到出错的地方,我想也许是PQ声明的问题.有人可以帮帮我吗?提前致谢!

izo*_*ica 6

看到这里.我引用:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

所有保证的是,根据比较器,顶部元素是最小元素.