28 |
|
* elements are added to a priority queue, its capacity grows |
29 |
|
* automatically. The details of the growth policy are not specified. |
30 |
|
* |
31 |
+ |
* <p>The Iterator provided in method {@link #iterator()} is <em>not</em> |
32 |
+ |
* guaranteed to traverse the elements of the PriorityQueue in any |
33 |
+ |
* particular order. If you need ordered traversal, consider using |
34 |
+ |
* <tt>Arrays.sort(pq.toArray())</tt>. |
35 |
+ |
* |
36 |
+ |
* <p> <strong>Note that this implementation is not synchronized.</strong> |
37 |
+ |
* Multiple threads should not access a <tt>PriorityQueue</tt> |
38 |
+ |
* instance concurrently if any of the threads modifies the list |
39 |
+ |
* structurally. Instead, use the thread-safe {@link |
40 |
+ |
* java.util.concurrent.BlockingPriorityQueue} class. |
41 |
+ |
* |
42 |
+ |
* |
43 |
|
* <p>Implementation note: this implementation provides O(log(n)) time |
44 |
|
* for the insertion methods (<tt>offer</tt>, <tt>poll</tt>, |
45 |
|
* <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the |
56 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
57 |
|
implements Queue<E>, java.io.Serializable { |
58 |
|
|
59 |
+ |
static final long serialVersionUID = -7720805057305804111L; |
60 |
+ |
|
61 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
62 |
|
|
63 |
|
/** |
95 |
|
/** |
96 |
|
* Creates a <tt>PriorityQueue</tt> with the default initial capacity |
97 |
|
* (11) that orders its elements according to their natural |
98 |
< |
* ordering (using <tt>Comparable</tt>.) |
98 |
> |
* ordering (using <tt>Comparable</tt>). |
99 |
|
*/ |
100 |
|
public PriorityQueue() { |
101 |
|
this(DEFAULT_INITIAL_CAPACITY, null); |
104 |
|
/** |
105 |
|
* Creates a <tt>PriorityQueue</tt> with the specified initial capacity |
106 |
|
* that orders its elements according to their natural ordering |
107 |
< |
* (using <tt>Comparable</tt>.) |
107 |
> |
* (using <tt>Comparable</tt>). |
108 |
|
* |
109 |
|
* @param initialCapacity the initial capacity for this priority queue. |
110 |
|
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
173 |
|
* specified collection. The priority queue has an initial |
174 |
|
* capacity of 110% of the size of the specified collection or 1 |
175 |
|
* if the collection is empty. If the specified collection is an |
176 |
< |
* instance of a {@link SortedSet} or is another |
176 |
> |
* instance of a {@link java.util.SortedSet} or is another |
177 |
|
* <tt>PriorityQueue</tt>, the priority queue will be sorted |
178 |
|
* according to the same comparator, or according to its elements' |
179 |
|
* natural order if the collection is sorted according to its |
190 |
|
*/ |
191 |
|
public PriorityQueue(Collection<? extends E> c) { |
192 |
|
initializeArray(c); |
193 |
< |
if (c instanceof SortedSet<? extends E>) { |
194 |
< |
SortedSet<? extends E> s = (SortedSet<? extends E>) c; |
193 |
> |
if (c instanceof SortedSet) { |
194 |
> |
// @fixme double-cast workaround for compiler |
195 |
> |
SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c; |
196 |
|
comparator = (Comparator<? super E>)s.comparator(); |
197 |
|
fillFromSorted(s); |
198 |
< |
} |
184 |
< |
else if (c instanceof PriorityQueue<? extends E>) { |
198 |
> |
} else if (c instanceof PriorityQueue) { |
199 |
|
PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c; |
200 |
|
comparator = (Comparator<? super E>)s.comparator(); |
201 |
|
fillFromSorted(s); |
202 |
< |
} |
189 |
< |
else { |
202 |
> |
} else { |
203 |
|
comparator = null; |
204 |
|
fillFromUnsorted(c); |
205 |
|
} |
544 |
|
/** |
545 |
|
* Returns the comparator used to order this collection, or <tt>null</tt> |
546 |
|
* if this collection is sorted according to its elements natural ordering |
547 |
< |
* (using <tt>Comparable</tt>.) |
547 |
> |
* (using <tt>Comparable</tt>). |
548 |
|
* |
549 |
|
* @return the comparator used to order this collection, or <tt>null</tt> |
550 |
|
* if this collection is sorted according to its elements natural ordering. |