Build 1.0_r1(from source)

java.util
Class PriorityQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by java.util.PriorityQueue<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>

public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable

PriorityQueue holds elements on a priority heap, which orders elements according to the comparator specified at construction or their natural order. If the queue uses natural order, any element that is not comparable is not permitted to insert to the queue. The least element of the specified ordering is stored at the head of the queue and the greatest element is stored at the tail of the queue. PriorityQueue is not synchronized. If multiple threads will access it concurrently, use the PriorityBlockingQueue.

See Also:
Serialized Form

Constructor Summary
PriorityQueue()
          Constructs a priority queue with the capacity of 11 and natural ordering.
PriorityQueue(Collection<? extends E> c)
          Constructs a priority queue that contains the elements of a collection.
PriorityQueue(int initialCapacity)
          Constructs a priority queue with specified capacity and natural ordering.
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
          Constructs a priority queue with specified capacity and comparator.
PriorityQueue(PriorityQueue<? extends E> c)
          Constructs a priority queue that contains the elements of another priority queue.
PriorityQueue(SortedSet<? extends E> c)
          Constructs a priority queue that contains the elements of a sorted set.
 
Method Summary
 boolean add(E o)
          Adds the specified object to the priority queue.
 void clear()
          Removes all the elements of the priority queue.
 Comparator<? super E> comparator()
          Gets the comparator of the priority queue.
 Iterator<E> iterator()
          Gets the iterator of the priority queue, which will not return elements in any specified ordering.
 boolean offer(E o)
          Inserts the element to the priority queue.
 E peek()
          Gets but not removes the head of the queue.
 E poll()
          Gets and removes the head of the queue.
 boolean remove(Object o)
          Removes the specified object of the priority queue.
 int size()
          Gets the size of the priority queue.
 
Methods inherited from class java.util.AbstractQueue
addAll, element, remove
 
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

PriorityQueue

public PriorityQueue()
Constructs a priority queue with the capacity of 11 and natural ordering.


PriorityQueue

public PriorityQueue(int initialCapacity)
Constructs a priority queue with specified capacity and natural ordering.

Parameters:
initialCapacity - the specified capacity.
Throws:
IllegalArgumentException - if the initialCapacity is less than 1

PriorityQueue

public PriorityQueue(int initialCapacity,
                     Comparator<? super E> comparator)
Constructs a priority queue with specified capacity and comparator.

Parameters:
initialCapacity - the specified capacity.
comparator - the specified comparator. If it is null, the natural ordering will be used.
Throws:
IllegalArgumentException - if the initialCapacity is less than 1

PriorityQueue

public PriorityQueue(Collection<? extends E> c)
Constructs a priority queue that contains the elements of a collection. The constructed priority queue has the initial capacity of 110% the collection. And the priority queue uses natural ordering to order its elements.

Parameters:
c - the collection whose elements will be added to the priority queue to be constructed.
Throws:
ClassCastException - if any of the elements in the collection is not comparable.
NullPointerExcepiton - if any of the elements in the collection is null.

PriorityQueue

public PriorityQueue(PriorityQueue<? extends E> c)
Constructs a priority queue that contains the elements of another priority queue. The constructed priority queue has the initial capacity of 110% the latter one. And the two priority queue has the same comparator.

Parameters:
c - the priority queue whose elements will be added to the priority queue to be constructed.

PriorityQueue

public PriorityQueue(SortedSet<? extends E> c)
Constructs a priority queue that contains the elements of a sorted set. The constructed priority queue has the initial capacity of 110% the sorted set. And the priority queue has the same comparator of the sorted set.

Parameters:
c - the sorted set whose elements will be added to the priority queue to be constructed.
Method Detail

iterator

public Iterator<E> iterator()
Gets the iterator of the priority queue, which will not return elements in any specified ordering.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>
Returns:
the iterator of the priority queue.
See Also:
Iterator

size

public int size()
Gets the size of the priority queue. If the size of the queue is greater than the Integer.MAX, then it returns Integer.MAX.

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the size of the priority queue.

clear

public void clear()
Removes all the elements of the priority queue.

Specified by:
clear in interface Collection<E>
Overrides:
clear in class AbstractQueue<E>
See Also:
Collection.isEmpty(), Collection.size()

offer

public boolean offer(E o)
Inserts the element to the priority queue.

Specified by:
offer in interface Queue<E>
Parameters:
o - the specified element to insert into the queue.
Returns:
true
Throws:
ClassCastException - if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerExcepiton - if the element is null.

poll

public E poll()
Gets and removes the head of the queue.

Specified by:
poll in interface Queue<E>
Returns:
the head of the queue. Null if the queue is empty.

peek

public E peek()
Gets but not removes the head of the queue.

Specified by:
peek in interface Queue<E>
Returns:
the head of the queue. Null if the queue is empty.

comparator

public Comparator<? super E> comparator()
Gets the comparator of the priority queue.

Returns:
the comparator of the priority queue. Null if the natural ordering is used.

remove

public boolean remove(Object o)
Removes the specified object of the priority queue.

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>
Parameters:
o - the object to be removed.
Returns:
true if the object is in the priority queue, false if the object is not in the priority queue.

add

public boolean add(E o)
Adds the specified object to the priority queue.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractQueue<E>
Parameters:
o - the object to be added.
Returns:
true.
Throws:
ClassCastException - if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerExcepiton - if the element is null.

Build 1.0_r1(from source)

Please submit a feedback, bug or feature