0

我有 PriorityQueue 使用示例,它产生

3

1

1

1

5

0

这是代码

import java.util.*;

class Someclass
{

public static class IntegerWr 
    implements Comparable<IntegerWr>
{
    Integer val;

    IntegerWr(Integer val)
    {
        this.val = val; 
    }

    public void change(Integer nval)
    { 
        this.val = nval;
    }

    @Override
    public int compareTo(IntegerWr iw)
    {
        return val.compareTo(iw.val);
    }
    @Override public String toString()
    {
        return ""+val;
    }
}
    public static void main (String[] args) 
    {
        PriorityQueue<IntegerWr> pq = new PriorityQueue<>();
        pq.add(new IntegerWr(3));
        System.out.println(pq.peek());
        IntegerWr iw1 = new IntegerWr(1);        
        pq.add(iw1);
        System.out.println(pq.peek());
        pq.add(new IntegerWr(4));
        System.out.println(pq.peek());
        pq.add(new IntegerWr(2));
        System.out.println(pq.peek()); //must output 1, and does so
        iw1.change(5);                 //change value of element that is actually on peek
        System.out.println(pq.peek()); //outputs 5 which is unexpected
        pq.add(new IntegerWr(0));
        System.out.println(pq.peek()); 
    }
}

似乎 PriorityQueue 仅在插入时订购。使用什么方法来获取实际的 peek()?

4

3 回答 3

1

您正在更改存储在队列中的对象内部的值。队列对对象的内容一无所知。因此,当您在队列中的对象上调用方法时(如在 'iw1.change(5)' 中),队列中的任何内容都不知道。您需要存储一个替换对象,以便队列重新排序元素。

于 2016-03-21T12:24:29.237 回答
1

而不是iw1.change(5);这样做:

pq.remove(iw1);
iw1.change(5);
pq.add(iw1);
于 2016-03-21T12:30:02.317 回答
0

PriorityQueue 是队列的一个实现。如果我们查看Queue 接口,它有 peek()、poll()、remove() 方法。

peek()方法返回但不移除队列的头部。

poll()方法移除并返回队列的头部。从队列中删除的确切元素取决于队列的排序策略。

import java.util.*;

class Someclass
{

public static class IntegerWr 
    implements Comparable<IntegerWr>
{
    Integer val;

    IntegerWr(Integer val)
    {
        this.val = val; 
    }

    public void change(Integer nval)
    { 
        this.val = nval;
    }

    @Override
    public int compareTo(IntegerWr iw)
    {
        return val.compareTo(iw.val);
    }
    @Override public String toString()
    {
        return ""+val;
    }
}
    public static void main (String[] args) 
    {
        PriorityQueue<IntegerWr> pq = new PriorityQueue<>();
        pq.add(new IntegerWr(3));
        System.out.println(pq.peek());
        IntegerWr iw1 = new IntegerWr(1);        
        pq.add(iw1);
        System.out.println(pq.peek());
        pq.add(new IntegerWr(4));
        System.out.println(pq.peek());
        pq.add(new IntegerWr(2));
        System.out.println(pq.peek()); //must output 1, and does so
        iw1.change(5);                 //change value of element that is actually on peek
        System.out.println(pq.peek()); //outputs 5 which is unexpected
        pq.add(new IntegerWr(0));
        System.out.println(pq.peek()); 

        System.out.println("Elements ordered");
        Object o = null;
        while ((o = pq.poll()) != null) //poll() method removes and return
                                        //the head of the queue.
                                        //Exactly which element is removed 
                                        //from the queue is a function 
                                        //of the queue's ordering policy
        {
            System.out.println(o);
        }
    }
}

输出

3

1

1

1

5

0

Elements ordered

0

2

3

4

5

要按顺序获取 PriorityQueue 的元素,请使用poll().

于 2016-03-22T10:16:41.497 回答