简体   繁体   English

PriorityQueue更改poll()上的元素位置

[英]PriorityQueue changes elements positions on poll()

I got some problem with using a priority queue. 使用优先级队列时出现一些问题。 Unfortuneatly my google research didn't have a result :( I hope someone can help me. 不幸的是,我的Google研究没有结果:(我希望有人可以帮助我。

I'm using a priority queue to sort some objects by a specific attribute called "reachabilityDistance" which is a simple double value. 我正在使用优先级队列通过称为“ reachabilityDistance”的特定属性对某些对象进行排序,该属性是一个简单的double值。 Inserting objects into the queue works fine and results in a correct ordering. 将对象插入队列可以正常工作,并导致正确的排序。 But then I want to poll the first object of the queue and the order of the other object changes even if their values are staying the same. 但是,然后我想轮询队列的第一个对象,并且其他对象的顺序也会更改,即使它们的值保持不变。 What is wrong with that? 怎么了

private PriorityQueue<ClusteringObject> orderedSeedQueue;
...
orderedSeedQueue = new PriorityQueue<>(clusteringObjects.size(), new ReachabilityObjectComperator());
while (!orderedSeedQueue.isEmpty()) {
            clusteringObject = orderedSeedQueue.poll();
            calculateNeighborhood(clusteringObject, clusteringObjects);
            clusteringObject.setProcessed();
            clusteringObject.setCoreDistance(minNeighbors);
            resultClusterList.add(clusteringObject);
            updateSeedQueue(clusteringObject.getNeighbors(), clusteringObject);
        }

This is the code snippet where I am working with my priority queue. 这是我使用优先级队列的代码片段。 In the updateSeedQueue method some values are inserted respectivly updated in that queue (remove and add again). 在updateSeedQueue方法中,某些值会相应地插入到该队列中进行更新(删除并再次添加)。 This works fine for me but every time the poll() is performed all correct sorted entries are getting a wrong order. 这对我来说很好用,但是每次执行poll()时,所有正确排序的条目都会得到错误的顺序。

Here is my comparator: 这是我的比较器:

public class ReachabilityObjectComperator implements Comparator<ClusteringObject> {

@Override
public int compare(ClusteringObject x, ClusteringObject y) {
    if (x.getReachabilityDistance() < y.getReachabilityDistance()) {
        return -1;
    } else if(x.getReachabilityDistance() > y.getReachabilityDistance()) {
        return 1;
    } else {
        if(x.getMetadataIndex() < y.getMetadataIndex()) {
            return -1;
        } else if(x.getMetadataIndex() > y.getMetadataIndex()) {
            return 1;
        } else {
            return 0;
        }
    }
}

}

So my question again: why does the poll() changes the ordering of the remaining objects in this queue? 所以我的问题又来了:为什么poll()更改此队列中其余对象的顺序?

PriorityQueue only claims to make the first entry in correct order. PriorityQueue仅声明以正确的顺序进行第一个输入。 If you iterate over the PriorityQueue you can see all but the first element in any order and it may change as you add/remove entries. 如果您遍历PriorityQueue,则可以按任何顺序看到除第一个元素以外的所有元素,并且在添加/删除条目时它可能会更改。

From the Javadoc for PriorityQueue 从Javadoc for PriorityQueue

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. 不保证方法iterator()中提供的Iterator以任何特定顺序遍历优先级队列的元素。 If you need ordered traversal, consider using Arrays.sort(pq.toArray()). 如果需要有序遍历,请考虑使用Arrays.sort(pq.toArray())。

If you need correct ordering at all times I suggest using TreeSet. 如果您始终需要正确的订购顺序,建议您使用TreeSet。

NavigableSet<MyType> orderedSeedQueue = new TreeSet<>(new MyComparator());
// add elements

while(!orderSeedQueue.isEmpty()) {
     firstItem = orderSeedQueue.pollFirst();

However, depending on your use case it may be simpler to sort them. 但是,根据您的用例,对它们进行排序可能更简单。

List<MyType> types = ...
Collections.sort(types, new MyComparator());
for(MyType t : types) { // in sorted order

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM