14 |
|
* and {@link java.util.TreeMap}: elements are ordered either according to |
15 |
|
* their <i>natural order</i> (see {@link Comparable}), or according to a |
16 |
|
* {@link java.util.Comparator}, depending on which constructor is used. |
17 |
+ |
* |
18 |
|
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
19 |
|
* respect to the specified ordering. If multiple elements are tied for least |
20 |
|
* value, the head is one of those elements. A priority queue does not permit |
21 |
< |
* <tt>null</tt> elements. |
21 |
> |
* <tt>null</tt> elements. |
22 |
|
* |
23 |
|
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
24 |
|
* return the head of the queue. |
26 |
|
* <p>The {@link #element()} and {@link #peek()} methods return, but do |
27 |
|
* not delete, the head of the queue. |
28 |
|
* |
29 |
< |
* <p>A priority queue has a <i>capacity</i>. The capacity is the |
30 |
< |
* size of the array used internally to store the elements on the |
31 |
< |
* queue. |
32 |
< |
* It is always at least as large as the queue size. As |
33 |
< |
* elements are added to a priority queue, its capacity grows |
34 |
< |
* automatically. The details of the growth policy are not specified. |
29 |
> |
* <p>A priority queue is unbounded, but has a <i>capacity</i>. The |
30 |
> |
* capacity is the size of the array used internally to store the |
31 |
> |
* elements on the queue. It is always at least as large as the queue |
32 |
> |
* size. As elements are added to a priority queue, its capacity |
33 |
> |
* grows automatically. The details of the growth policy are not |
34 |
> |
* specified. |
35 |
|
* |
36 |
|
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
37 |
|
* guaranteed to traverse the elements of the PriorityQueue in any |
278 |
|
} |
279 |
|
|
280 |
|
|
280 |
– |
// Queue Methods |
281 |
– |
|
281 |
|
/** |
282 |
< |
* Add the specified element to this priority queue. |
282 |
> |
* Inserts the specified element to this priority queue. |
283 |
|
* |
284 |
|
* @return <tt>true</tt> |
285 |
|
* @throws ClassCastException if the specified element cannot be compared |
302 |
|
return true; |
303 |
|
} |
304 |
|
|
305 |
< |
public E poll() { |
305 |
> |
public E peek() { |
306 |
|
if (size == 0) |
307 |
|
return null; |
309 |
– |
return remove(); |
310 |
– |
} |
311 |
– |
|
312 |
– |
public E peek() { |
308 |
|
return (E) queue[1]; |
309 |
|
} |
310 |
|
|
315 |
|
* @return <tt>true</tt> (as per the general contract of |
316 |
|
* <tt>Collection.add</tt>). |
317 |
|
* |
318 |
< |
* @throws NullPointerException {@inheritDoc} |
318 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
319 |
|
* @throws ClassCastException if the specified element cannot be compared |
320 |
|
* with elements currently in the priority queue according |
321 |
|
* to the priority queue's ordering. |
334 |
|
* <p> |
335 |
|
* This implementation iterates over the specified collection, and adds |
336 |
|
* each object returned by the iterator to this collection, in turn. |
337 |
< |
* @throws NullPointerException {@inheritDoc} |
337 |
> |
* @param c collection whose elements are to be added to this queue |
338 |
> |
* @return <tt>true</tt> if this queue changed as a result of the |
339 |
> |
* call. |
340 |
> |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
341 |
> |
* is <tt>null</tt> |
342 |
|
* @throws ClassCastException if any element cannot be compared |
343 |
|
* with elements currently in the priority queue according |
344 |
|
* to the priority queue's ordering. |
347 |
|
return super.addAll(c); |
348 |
|
} |
349 |
|
|
351 |
– |
|
352 |
– |
/** |
353 |
– |
* Removes a single instance of the specified element from this |
354 |
– |
* queue, if it is present. More formally, |
355 |
– |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
356 |
– |
* o.equals(e))</tt>, if the queue contains one or more such |
357 |
– |
* elements. Returns <tt>true</tt> if the queue contained the |
358 |
– |
* specified element (or equivalently, if the queue changed as a |
359 |
– |
* result of the call). |
360 |
– |
* |
361 |
– |
* <p>This implementation iterates over the queue looking for the |
362 |
– |
* specified element. If it finds the element, it removes the element |
363 |
– |
* from the queue using the iterator's remove method.<p> |
364 |
– |
* |
365 |
– |
*/ |
350 |
|
public boolean remove(Object o) { |
351 |
|
if (o == null) |
352 |
|
return false; |
490 |
|
size = 0; |
491 |
|
} |
492 |
|
|
493 |
< |
/** |
510 |
< |
* Removes and returns the first element from queue. |
511 |
< |
*/ |
512 |
< |
public E remove() { |
493 |
> |
public E poll() { |
494 |
|
if (size == 0) |
495 |
< |
throw new NoSuchElementException(); |
495 |
> |
return null; |
496 |
|
modCount++; |
497 |
|
|
498 |
|
E result = (E) queue[1]; |