ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
(Generate patch)

Comparing jsr166/src/main/java/util/PriorityQueue.java (file contents):
Revision 1.7 by dl, Tue Jun 24 14:34:30 2003 UTC vs.
Revision 1.29 by dl, Sun Aug 24 23:31:53 2003 UTC

# Line 1 | Line 1
1   package java.util;
2  
3   /**
4 < * An unbounded priority queue based on a priority heap.  This queue orders
4 > * An unbounded priority {@linkplain Queue queue} based on a priority heap.  
5 > * This queue orders
6   * elements according to an order specified at construction time, which is
7 < * specified in the same manner as {@link TreeSet} and {@link TreeMap}: elements are ordered
7 > * specified in the same manner as {@link java.util.TreeSet} and
8 > * {@link java.util.TreeMap}: elements are ordered
9   * either according to their <i>natural order</i> (see {@link Comparable}), or
10 < * according to a {@link Comparator}, depending on which constructor is used.
11 < * The {@link #peek}, {@link #poll}, and {@link #remove} methods return the
12 < * minimal element with respect to the specified ordering.  If multiple
13 < * elements are tied for least value, no guarantees are made as to
14 < * which of these elements is returned.
10 > * according to a {@link java.util.Comparator}, depending on which
11 > * constructor is used.
12 > * <p>The <em>head</em> of this queue is the <em>least</em> element with
13 > * respect to the specified ordering.
14 > * If multiple elements are tied for least value, the
15 > * head is one of those elements. A priority queue does not permit
16 > * <tt>null</tt> elements.
17 > *
18 > * <p>The {@link #remove()} and {@link #poll()} methods remove and
19 > * return the head of the queue.
20 > *
21 > * <p>The {@link #element()} and {@link #peek()} methods return, but do
22 > * not delete, the head of the queue.
23   *
24   * <p>A priority queue has a <i>capacity</i>.  The capacity is the
25   * size of the array used internally to store the elements on the
26 < * queue.  It is always at least as large as the queue size.  As
26 > * queue.
27 > * It is always at least as large as the queue size.  As
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>Implementation note: this implementation provides O(log(n)) time
32 < *for the insertion methods (<tt>offer</tt>, <tt>poll</tt>,
33 < *<tt>remove()</tt> and <tt>add</tt>) methods; linear time for the
34 < *<tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and
35 < *constant time for the retrieval methods (<tt>peek</tt>,
36 < *<tt>element</tt>, and <tt>size</tt>).
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
46 > * <tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and
47 > * constant time for the retrieval methods (<tt>peek</tt>,
48 > * <tt>element</tt>, and <tt>size</tt>).
49   *
50   * <p>This class is a member of the
51   * <a href="{@docRoot}/../guide/collections/index.html">
# Line 31 | Line 54
54   * @author Josh Bloch
55   */
56   public class PriorityQueue<E> extends AbstractQueue<E>
57 <                              implements Queue<E> {
57 >    implements Queue<E>, java.io.Serializable {
58 >
59      private static final int DEFAULT_INITIAL_CAPACITY = 11;
60  
61      /**
# Line 47 | Line 71 | public class PriorityQueue<E> extends Ab
71       *
72       * queue.length must be >= 2, even if size == 0.
73       */
74 <    private transient E[] queue;
74 >    private transient Object[] queue;
75  
76      /**
77       * The number of elements in the priority queue.
# Line 58 | Line 82 | public class PriorityQueue<E> extends Ab
82       * The comparator, or null if priority queue uses elements'
83       * natural ordering.
84       */
85 <    private final Comparator<E> comparator;
85 >    private final Comparator<? super E> comparator;
86  
87      /**
88       * The number of times this priority queue has been
# Line 67 | Line 91 | public class PriorityQueue<E> extends Ab
91      private transient int modCount = 0;
92  
93      /**
94 <     * Create a new priority queue with the default initial capacity
94 >     * Creates a <tt>PriorityQueue</tt> with the default initial capacity
95       * (11) that orders its elements according to their natural
96 <     * ordering (using <tt>Comparable</tt>.)
96 >     * ordering (using <tt>Comparable</tt>).
97       */
98      public PriorityQueue() {
99 <        this(DEFAULT_INITIAL_CAPACITY);
99 >        this(DEFAULT_INITIAL_CAPACITY, null);
100      }
101  
102      /**
103 <     * Create a new priority queue with the specified initial capacity
103 >     * Creates a <tt>PriorityQueue</tt> with the specified initial capacity
104       * that orders its elements according to their natural ordering
105 <     * (using <tt>Comparable</tt>.)
105 >     * (using <tt>Comparable</tt>).
106       *
107       * @param initialCapacity the initial capacity for this priority queue.
108 +     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
109 +     * than 1
110       */
111      public PriorityQueue(int initialCapacity) {
112          this(initialCapacity, null);
113      }
114  
115      /**
116 <     * Create a new priority queue with the specified initial capacity (11)
116 >     * Creates a <tt>PriorityQueue</tt> with the specified initial capacity
117       * that orders its elements according to the specified comparator.
118       *
119       * @param initialCapacity the initial capacity for this priority queue.
120       * @param comparator the comparator used to order this priority queue.
121 +     * If <tt>null</tt> then the order depends on the elements' natural
122 +     * ordering.
123 +     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
124 +     * than 1
125       */
126 <    public PriorityQueue(int initialCapacity, Comparator<E> comparator) {
126 >    public PriorityQueue(int initialCapacity,
127 >                         Comparator<? super E> comparator) {
128          if (initialCapacity < 1)
129 <            initialCapacity = 1;
130 <        queue = new E[initialCapacity + 1];
129 >            throw new IllegalArgumentException();
130 >        this.queue = new Object[initialCapacity + 1];
131          this.comparator = comparator;
132      }
133  
134      /**
135 <     * Create a new priority queue containing the elements in the specified
136 <     * collection.  The priority queue has an initial capacity of 110% of the
106 <     * size of the specified collection. If the specified collection
107 <     * implements the {@link Sorted} interface, the priority queue will be
108 <     * sorted according to the same comparator, or according to its elements'
109 <     * natural order if the collection is sorted according to its elements'
110 <     * natural order.  If the specified collection does not implement
111 <     * <tt>Sorted</tt>, the priority queue is ordered according to
112 <     * its elements' natural order.
113 <     *
114 <     * @param initialElements the collection whose elements are to be placed
115 <     *        into this priority queue.
116 <     * @throws ClassCastException if elements of the specified collection
117 <     *         cannot be compared to one another according to the priority
118 <     *         queue's ordering.
119 <     * @throws NullPointerException if the specified collection or an
120 <     *         element of the specified collection is <tt>null</tt>.
135 >     * Common code to initialize underlying queue array across
136 >     * constructors below.
137       */
138 <    public PriorityQueue(Collection<E> initialElements) {
139 <        int sz = initialElements.size();
138 >    private void initializeArray(Collection<? extends E> c) {
139 >        int sz = c.size();
140          int initialCapacity = (int)Math.min((sz * 110L) / 100,
141                                              Integer.MAX_VALUE - 1);
142          if (initialCapacity < 1)
143              initialCapacity = 1;
128        queue = new E[initialCapacity + 1];
144  
145 <        /* Commented out to compile with generics compiler
145 >        this.queue = new Object[initialCapacity + 1];
146 >    }
147 >
148 >    /**
149 >     * Initially fill elements of the queue array under the
150 >     * knowledge that it is sorted or is another PQ, in which
151 >     * case we can just place the elements without fixups.
152 >     */
153 >    private void fillFromSorted(Collection<? extends E> c) {
154 >        for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
155 >            queue[++size] = i.next();
156 >    }
157 >
158 >
159 >    /**
160 >     * Initially fill elements of the queue array that is
161 >     * not to our knowledge sorted, so we must add them
162 >     * one by one.
163 >     */
164 >    private void fillFromUnsorted(Collection<? extends E> c) {
165 >        for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
166 >            add(i.next());
167 >    }
168  
169 <        if (initialElements instanceof Sorted) {
170 <            comparator = ((Sorted)initialElements).comparator();
171 <            for (Iterator<E> i = initialElements.iterator(); i.hasNext(); )
172 <                queue[++size] = i.next();
169 >    /**
170 >     * Creates a <tt>PriorityQueue</tt> containing the elements in the
171 >     * specified collection.  The priority queue has an initial
172 >     * capacity of 110% of the size of the specified collection or 1
173 >     * if the collection is empty.  If the specified collection is an
174 >     * instance of a {@link java.util.SortedSet} or is another
175 >     * <tt>PriorityQueue</tt>, the priority queue will be sorted
176 >     * according to the same comparator, or according to its elements'
177 >     * natural order if the collection is sorted according to its
178 >     * elements' natural order.  Otherwise, the priority queue is
179 >     * ordered according to its elements' natural order.
180 >     *
181 >     * @param c the collection whose elements are to be placed
182 >     *        into this priority queue.
183 >     * @throws ClassCastException if elements of the specified collection
184 >     *         cannot be compared to one another according to the priority
185 >     *         queue's ordering.
186 >     * @throws NullPointerException if <tt>c</tt> or any element within it
187 >     * is <tt>null</tt>
188 >     */
189 >    public PriorityQueue(Collection<? extends E> c) {
190 >        initializeArray(c);
191 >        if (c instanceof SortedSet) {
192 >            // @fixme double-cast workaround for compiler
193 >            SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c;
194 >            comparator = (Comparator<? super E>)s.comparator();
195 >            fillFromSorted(s);
196 >        } else if (c instanceof PriorityQueue) {
197 >            PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c;
198 >            comparator = (Comparator<? super E>)s.comparator();
199 >            fillFromSorted(s);
200          } else {
137        */
138        {
201              comparator = null;
202 <            for (Iterator<E> i = initialElements.iterator(); i.hasNext(); )
141 <                add(i.next());
202 >            fillFromUnsorted(c);
203          }
204      }
205  
206 +    /**
207 +     * Creates a <tt>PriorityQueue</tt> containing the elements in the
208 +     * specified collection.  The priority queue has an initial
209 +     * capacity of 110% of the size of the specified collection or 1
210 +     * if the collection is empty.  This priority queue will be sorted
211 +     * according to the same comparator as the given collection, or
212 +     * according to its elements' natural order if the collection is
213 +     * sorted according to its elements' natural order.
214 +     *
215 +     * @param c the collection whose elements are to be placed
216 +     *        into this priority queue.
217 +     * @throws ClassCastException if elements of the specified collection
218 +     *         cannot be compared to one another according to the priority
219 +     *         queue's ordering.
220 +     * @throws NullPointerException if <tt>c</tt> or any element within it
221 +     * is <tt>null</tt>
222 +     */
223 +    public PriorityQueue(PriorityQueue<? extends E> c) {
224 +        initializeArray(c);
225 +        comparator = (Comparator<? super E>)c.comparator();
226 +        fillFromSorted(c);
227 +    }
228 +
229 +    /**
230 +     * Creates a <tt>PriorityQueue</tt> containing the elements in the
231 +     * specified collection.  The priority queue has an initial
232 +     * capacity of 110% of the size of the specified collection or 1
233 +     * if the collection is empty.  This priority queue will be sorted
234 +     * according to the same comparator as the given collection, or
235 +     * according to its elements' natural order if the collection is
236 +     * sorted according to its elements' natural order.
237 +     *
238 +     * @param c the collection whose elements are to be placed
239 +     *        into this priority queue.
240 +     * @throws ClassCastException if elements of the specified collection
241 +     *         cannot be compared to one another according to the priority
242 +     *         queue's ordering.
243 +     * @throws NullPointerException if <tt>c</tt> or any element within it
244 +     * is <tt>null</tt>
245 +     */
246 +    public PriorityQueue(SortedSet<? extends E> c) {
247 +        initializeArray(c);
248 +        comparator = (Comparator<? super E>)c.comparator();
249 +        fillFromSorted(c);
250 +    }
251 +
252 +    /**
253 +     * Resize array, if necessary, to be able to hold given index
254 +     */
255 +    private void grow(int index) {
256 +        int newlen = queue.length;
257 +        if (index < newlen) // don't need to grow
258 +            return;
259 +        if (index == Integer.MAX_VALUE)
260 +            throw new OutOfMemoryError();
261 +        while (newlen <= index) {
262 +            if (newlen >= Integer.MAX_VALUE / 2)  // avoid overflow
263 +                newlen = Integer.MAX_VALUE;
264 +            else
265 +                newlen <<= 2;
266 +        }
267 +        Object[] newQueue = new Object[newlen];
268 +        System.arraycopy(queue, 0, newQueue, 0, queue.length);
269 +        queue = newQueue;
270 +    }
271 +            
272      // Queue Methods
273  
274 +
275 +
276      /**
277 <     * Remove and return the minimal element from this priority queue
149 <     * if it contains one or more elements, otherwise return
150 <     * <tt>null</tt>.  The term <i>minimal</i> is defined according to
151 <     * this priority queue's order.
277 >     * Add the specified element to this priority queue.
278       *
279 <     * @return the minimal element from this priority queue if it contains
280 <     *         one or more elements, otherwise <tt>null</tt>.
279 >     * @return <tt>true</tt>
280 >     * @throws ClassCastException if the specified element cannot be compared
281 >     * with elements currently in the priority queue according
282 >     * to the priority queue's ordering.
283 >     * @throws NullPointerException if the specified element is <tt>null</tt>.
284       */
285 +    public boolean offer(E o) {
286 +        if (o == null)
287 +            throw new NullPointerException();
288 +        modCount++;
289 +        ++size;
290 +
291 +        // Grow backing store if necessary
292 +        if (size >= queue.length)
293 +            grow(size);
294 +
295 +        queue[size] = o;
296 +        fixUp(size);
297 +        return true;
298 +    }
299 +
300      public E poll() {
301          if (size == 0)
302              return null;
303 <        return remove(1);
303 >        return (E) remove(1);
304 >    }
305 >
306 >    public E peek() {
307 >        return (E) queue[1];
308      }
309  
310 +    // Collection Methods - the first two override to update docs
311 +
312      /**
313 <     * Return, but do not remove, the minimal element from the
314 <     * priority queue, or return <tt>null</tt> if the queue is empty.
315 <     * The term <i>minimal</i> is defined according to this priority
166 <     * queue's order.  This method returns the same object reference
167 <     * that would be returned by by the <tt>poll</tt> method.  The two
168 <     * methods differ in that this method does not remove the element
169 <     * from the priority queue.
313 >     * Adds the specified element to this queue.
314 >     * @return <tt>true</tt> (as per the general contract of
315 >     * <tt>Collection.add</tt>).
316       *
317 <     * @return the minimal element from this priority queue if it contains
318 <     *         one or more elements, otherwise <tt>null</tt>.
317 >     * @throws NullPointerException {@inheritDoc}
318 >     * @throws ClassCastException if the specified element cannot be compared
319 >     * with elements currently in the priority queue according
320 >     * to the priority queue's ordering.
321       */
322 <    public E peek() {
323 <        return queue[1];
322 >    public boolean add(E o) {
323 >        return super.add(o);
324      }
325  
326 <    // Collection Methods
179 <
326 >  
327      /**
328 <     * Removes a single instance of the specified element from this priority
329 <     * queue, if it is present.  Returns true if this collection contained the
330 <     * specified element (or equivalently, if this collection changed as a
328 >     * Adds all of the elements in the specified collection to this queue.
329 >     * The behavior of this operation is undefined if
330 >     * the specified collection is modified while the operation is in
331 >     * progress.  (This implies that the behavior of this call is undefined if
332 >     * the specified collection is this queue, and this queue is nonempty.)
333 >     * <p>
334 >     * This implementation iterates over the specified collection, and adds
335 >     * each object returned by the iterator to this collection, in turn.
336 >     * @throws NullPointerException {@inheritDoc}
337 >     * @throws ClassCastException if any element cannot be compared
338 >     * with elements currently in the priority queue according
339 >     * to the priority queue's ordering.
340 >     */
341 >    public boolean addAll(Collection<? extends E> c) {
342 >        return super.addAll(c);
343 >    }
344 >
345 >
346 > /**
347 >     * Removes a single instance of the specified element from this
348 >     * queue, if it is present.  More formally,
349 >     * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
350 >     * o.equals(e))</tt>, if the queue contains one or more such
351 >     * elements.  Returns <tt>true</tt> if the queue contained the
352 >     * specified element (or equivalently, if the queue changed as a
353       * result of the call).
354       *
355 <     * @param element the element to be removed from this collection,
356 <     * if present.
357 <     * @return <tt>true</tt> if this collection changed as a result of the
358 <     *         call
190 <     * @throws ClassCastException if the specified element cannot be compared
191 <     *            with elements currently in the priority queue according
192 <     *            to the priority queue's ordering.
193 <     * @throws NullPointerException if the specified element is null.
355 >     * <p>This implementation iterates over the queue looking for the
356 >     * specified element.  If it finds the element, it removes the element
357 >     * from the queue using the iterator's remove method.<p>
358 >     *
359       */
360 <    public boolean remove(Object element) {
361 <        if (element == null)
362 <            throw new NullPointerException();
360 >    public boolean remove(Object o) {
361 >        if (o == null)
362 >            return false;
363  
364          if (comparator == null) {
365              for (int i = 1; i <= size; i++) {
366 <                if (((Comparable)queue[i]).compareTo(element) == 0) {
366 >                if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) {
367                      remove(i);
368                      return true;
369                  }
370              }
371          } else {
372              for (int i = 1; i <= size; i++) {
373 <                if (comparator.compare(queue[i], (E) element) == 0) {
373 >                if (comparator.compare((E)queue[i], (E)o) == 0) {
374                      remove(i);
375                      return true;
376                  }
# Line 215 | Line 380 | public class PriorityQueue<E> extends Ab
380      }
381  
382      /**
383 <     * Returns an iterator over the elements in this priority queue.  The
384 <     * elements of the priority queue will be returned by this iterator in the
385 <     * order specified by the queue, which is to say the order they would be
386 <     * returned by repeated calls to <tt>poll</tt>.
222 <     *
223 <     * @return an <tt>Iterator</tt> over the elements in this priority queue.
383 >     * Returns an iterator over the elements in this queue. The iterator
384 >     * does not return the elements in any particular order.
385 >     *
386 >     * @return an iterator over the elements in this queue.
387       */
388      public Iterator<E> iterator() {
389          return new Itr();
# Line 255 | Line 418 | public class PriorityQueue<E> extends Ab
418              checkForComodification();
419              if (cursor > size)
420                  throw new NoSuchElementException();
421 <            E result = queue[cursor];
421 >            E result = (E) queue[cursor];
422              lastRet = cursor++;
423              return result;
424          }
# Line 278 | Line 441 | public class PriorityQueue<E> extends Ab
441          }
442      }
443  
281    /**
282     * Returns the number of elements in this priority queue.
283     *
284     * @return the number of elements in this priority queue.
285     */
444      public int size() {
445          return size;
446      }
447  
448      /**
291     * Add the specified element to this priority queue.
292     *
293     * @param element the element to add.
294     * @return true
295     * @throws ClassCastException if the specified element cannot be compared
296     *            with elements currently in the priority queue according
297     *            to the priority queue's ordering.
298     * @throws NullPointerException if the specified element is null.
299     */
300    public boolean offer(E element) {
301        if (element == null)
302            throw new NullPointerException();
303        modCount++;
304
305        // Grow backing store if necessary
306        if (++size == queue.length) {
307            E[] newQueue = new E[2 * queue.length];
308            System.arraycopy(queue, 0, newQueue, 0, size);
309            queue = newQueue;
310        }
311
312        queue[size] = element;
313        fixUp(size);
314        return true;
315    }
316
317    /**
449       * Remove all elements from the priority queue.
450       */
451      public void clear() {
# Line 338 | Line 469 | public class PriorityQueue<E> extends Ab
469          assert i <= size;
470          modCount++;
471  
472 <        E result = queue[i];
472 >        E result = (E) queue[i];
473          queue[i] = queue[size];
474          queue[size--] = null;  // Drop extra ref to prevent memory leak
475          if (i <= size)
# Line 359 | Line 490 | public class PriorityQueue<E> extends Ab
490          if (comparator == null) {
491              while (k > 1) {
492                  int j = k >> 1;
493 <                if (((Comparable)queue[j]).compareTo(queue[k]) <= 0)
493 >                if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0)
494                      break;
495 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
495 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
496                  k = j;
497              }
498          } else {
499              while (k > 1) {
500                  int j = k >> 1;
501 <                if (comparator.compare(queue[j], queue[k]) <= 0)
501 >                if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
502                      break;
503 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
503 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
504                  k = j;
505              }
506          }
# Line 388 | Line 519 | public class PriorityQueue<E> extends Ab
519          int j;
520          if (comparator == null) {
521              while ((j = k << 1) <= size) {
522 <                if (j<size && ((Comparable)queue[j]).compareTo(queue[j+1]) > 0)
522 >                if (j<size && ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0)
523                      j++; // j indexes smallest kid
524 <                if (((Comparable)queue[k]).compareTo(queue[j]) <= 0)
524 >                if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0)
525                      break;
526 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
526 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
527                  k = j;
528              }
529          } else {
530              while ((j = k << 1) <= size) {
531 <                if (j < size && comparator.compare(queue[j], queue[j+1]) > 0)
531 >                if (j < size && comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
532                      j++; // j indexes smallest kid
533 <                if (comparator.compare(queue[k], queue[j]) <= 0)
533 >                if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)
534                      break;
535 <                E tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
535 >                Object tmp = queue[j];  queue[j] = queue[k]; queue[k] = tmp;
536                  k = j;
537              }
538          }
539      }
540  
541 +
542      /**
543 <     * Returns the comparator associated with this priority queue, or
544 <     * <tt>null</tt> if it uses its elements' natural ordering.
543 >     * Returns the comparator used to order this collection, or <tt>null</tt>
544 >     * if this collection is sorted according to its elements natural ordering
545 >     * (using <tt>Comparable</tt>).
546       *
547 <     * @return the comparator associated with this priority queue, or
548 <     *         <tt>null</tt> if it uses its elements' natural ordering.
547 >     * @return the comparator used to order this collection, or <tt>null</tt>
548 >     * if this collection is sorted according to its elements natural ordering.
549       */
550 <    Comparator comparator() {
550 >    public Comparator<? super E> comparator() {
551          return comparator;
552      }
553  
# Line 427 | Line 560 | public class PriorityQueue<E> extends Ab
560       * <tt>Object</tt>) in the proper order.
561       * @param s the stream
562       */
563 <    private synchronized void writeObject(java.io.ObjectOutputStream s)
563 >    private void writeObject(java.io.ObjectOutputStream s)
564          throws java.io.IOException{
565          // Write out element count, and any hidden stuff
566          s.defaultWriteObject();
# Line 445 | Line 578 | public class PriorityQueue<E> extends Ab
578       * deserialize it).
579       * @param s the stream
580       */
581 <    private synchronized void readObject(java.io.ObjectInputStream s)
581 >    private void readObject(java.io.ObjectInputStream s)
582          throws java.io.IOException, ClassNotFoundException {
583          // Read in size, and any hidden stuff
584          s.defaultReadObject();
585  
586          // Read in array length and allocate array
587          int arrayLength = s.readInt();
588 <        queue = new E[arrayLength];
588 >        queue = new Object[arrayLength];
589  
590          // Read in all elements in the proper order.
591          for (int i=0; i<size; i++)
592 <            queue[i] = (E)s.readObject();
592 >            queue[i] = s.readObject();
593      }
594  
595   }
596 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines