偷看()或不偷看()
To peek() or not to 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()?
您正在更改存储在队列中的对象内部的值。
队列对对象的内容一无所知。
因此,当您对队列中的对象调用方法时(如 'iw1.change(5)'),队列中的任何人都不知道它。
您需要存储替换对象,以便队列重新排序元素。
代替iw1.change(5);
做:
pq.remove(iw1);
iw1.change(5);
pq.add(iw1);
PriorityQueue 是队列的一个实现。如果我们查看 Queue interface,它有方法 peek()、poll()、remove()。
peek()
方法 returns,但不删除队列的头部。
poll()
方法删除并 return 队列的头部。究竟从队列中删除哪个元素是队列排序策略的函数。
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()
.
我有生成
的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()?
您正在更改存储在队列中的对象内部的值。 队列对对象的内容一无所知。 因此,当您对队列中的对象调用方法时(如 'iw1.change(5)'),队列中的任何人都不知道它。 您需要存储替换对象,以便队列重新排序元素。
代替iw1.change(5);
做:
pq.remove(iw1);
iw1.change(5);
pq.add(iw1);
PriorityQueue 是队列的一个实现。如果我们查看 Queue interface,它有方法 peek()、poll()、remove()。
peek()
方法 returns,但不删除队列的头部。
poll()
方法删除并 return 队列的头部。究竟从队列中删除哪个元素是队列排序策略的函数。
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()
.