偷看()或不偷看()

时间:2016-03-21 12:13:26

标签: java collections priority-queue peek

我有生成

的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()?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

pq.remove(iw1);
iw1.change(5);
pq.add(iw1);

答案 2 :(得分:0)

PriorityQueue是Qu​​eue的一个实现。如果我们查看Queue interface它有方法peek(),poll(),remove()。

Me!ItemName = DLookup("ItemName", "[my Table]", "ItemCode = '" & Me![ItemCode] & "'") 方法返回,但不删除队列的头部。

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);
        }
    }
}

要按顺序获取PriorityQueue 的元素,请使用3 1 1 1 5 0 Elements ordered 0 2 3 4 5