简体   繁体   English

窥视()还是不窥视()

[英]To peek() or not to peek()

I have PriorityQueue use example which produces 我有PriorityQueue使用示例,它产生

3

1

1

1

5

0

This is the code 这是代码

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

Seems like the PriorityQueue orders only on insert. 似乎PriorityQueue仅在插入时下订单。 What method to use to get the actual peek()? 使用什么方法来获取实际的peek()?

You are changing a value INSIDE the object stored in the queue. 您正在更改队列中存储的对象内部的值。 The queue does not know anything about the contents of the objects. 队列对对象的内容一无所知。 So when you call a method on an object in the queue (as in 'iw1.change(5)'), nothing in the queue knows about it. 因此,当您在队列中的对象上调用方法时(例如在“ iw1.change(5)”中),队列中的任何人都不知道。 You need to store a replacement object, for the queue to re-order the elements. 您需要存储一个替换对象,以便队列对元素重新排序。

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

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

PriorityQueue is an implementation of the Queue. PriorityQueue是Qu​​eue的实现。 If we look at the Queue interface it has methods peek(), poll(), remove(). 如果我们查看Queue接口,它具有方法peek(),poll(),remove()。

peek() method returns, but does not remove, the head of the queue. peek()方法返回但不删除队列的开头。

poll() method removes and return the head of the queue. poll()方法删除并返回队列的头部。 Exactly which element is removed from the queue is a function of the queue's ordering policy. 确切地说,从队列中删除了哪个元素是队列的排序策略的函数。

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

Output 输出量

3

1

1

1

5

0

Elements ordered

0

2

3

4

5

To get elements of PriorityQueue in order use poll() . 要按顺序获取PriorityQueue的元素,请使用poll()

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

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