8 |
|
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
< |
* An unbounded priority {@linkplain Queue queue} based on a priority heap. |
12 |
< |
* This queue orders elements according to an order specified at construction |
13 |
< |
* time, which is specified in the same manner as {@link java.util.TreeSet} |
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. |
11 |
> |
* An unbounded priority {@linkplain Queue queue} based on a priority |
12 |
> |
* heap. This queue orders elements according to an order specified |
13 |
> |
* at construction time, which is specified either according to their |
14 |
> |
* <i>natural order</i> (see {@link Comparable}), or according to a |
15 |
> |
* {@link java.util.Comparator}, depending on which constructor is |
16 |
> |
* used. A priority queue does not permit <tt>null</tt> elements. |
17 |
> |
* A priority queue relying on natural ordering also does not |
18 |
> |
* permit insertion of non-comparable objects (doing so may result |
19 |
> |
* in <tt>ClassCastException</tt>). |
20 |
|
* |
21 |
< |
* <p>The <em>head</em> of this queue is the <em>least</em> element with |
22 |
< |
* respect to the specified ordering. If multiple elements are tied for least |
23 |
< |
* value, the head is one of those elements. A priority queue does not permit |
24 |
< |
* <tt>null</tt> elements. |
21 |
> |
* <p>The <em>head</em> of this queue is the <em>least</em> element |
22 |
> |
* with respect to the specified ordering. If multiple elements are |
23 |
> |
* tied for least value, the head is one of those elements -- ties are |
24 |
> |
* broken arbitrarily. The queue retrieval operations <tt>poll</tt>, |
25 |
> |
* <tt>remove</tt>, <tt>peek</tt>, and <tt>element</tt> access the |
26 |
> |
* element at the head of the queue. |
27 |
|
* |
28 |
< |
* <p>The {@link #remove()} and {@link #poll()} methods remove and |
29 |
< |
* return the head of the queue. |
25 |
< |
* |
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 is unbounded, but has a <i>capacity</i>. The |
30 |
< |
* capacity is the size of the array used internally to store the |
28 |
> |
* <p>A priority queue is unbounded, but has an internal |
29 |
> |
* <i>capacity</i> governing the size of an array used to store the |
30 |
|
* elements on the queue. It is always at least as large as the queue |
31 |
|
* size. As elements are added to a priority queue, its capacity |
32 |
|
* grows automatically. The details of the growth policy are not |
33 |
|
* specified. |
34 |
|
* |
35 |
< |
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
35 |
> |
* <p>This class implements all of the <em>optional</em> methods of |
36 |
> |
* the {@link Collection} and {@link Iterator} interfaces. The |
37 |
> |
* Iterator provided in method {@link #iterator()} is <em>not</em> |
38 |
|
* guaranteed to traverse the elements of the PriorityQueue in any |
39 |
|
* particular order. If you need ordered traversal, consider using |
40 |
|
* <tt>Arrays.sort(pq.toArray())</tt>. |
59 |
|
* @since 1.5 |
60 |
|
* @version %I%, %G% |
61 |
|
* @author Josh Bloch |
62 |
+ |
* @param <E> the type of elements held in this collection |
63 |
|
*/ |
64 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
65 |
|
implements Queue<E>, java.io.Serializable { |
281 |
|
|
282 |
|
|
283 |
|
/** |
284 |
< |
* Inserts the specified element to this priority queue. |
284 |
> |
* Inserts the specified element into this priority queue. |
285 |
|
* |
286 |
|
* @return <tt>true</tt> |
287 |
|
* @throws ClassCastException if the specified element cannot be compared |
323 |
|
* to the priority queue's ordering. |
324 |
|
*/ |
325 |
|
public boolean add(E o) { |
326 |
< |
return super.add(o); |
325 |
< |
} |
326 |
< |
|
327 |
< |
|
328 |
< |
/** |
329 |
< |
* Adds all of the elements in the specified collection to this queue. |
330 |
< |
* The behavior of this operation is undefined if |
331 |
< |
* the specified collection is modified while the operation is in |
332 |
< |
* progress. (This implies that the behavior of this call is undefined if |
333 |
< |
* the specified collection is this queue, and this queue is nonempty.) |
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 |
< |
* @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. |
345 |
< |
*/ |
346 |
< |
public boolean addAll(Collection<? extends E> c) { |
347 |
< |
return super.addAll(c); |
326 |
> |
return offer(o); |
327 |
|
} |
328 |
|
|
329 |
|
public boolean remove(Object o) { |