fi.joensuu.cs.tra
Class PriorityQueue<E>

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

public class PriorityQueue<E>
extends PriorityQueue<E>

PriorityQueue is a convenience extension of java.util.PriorityQueue for relocation. PriorityQueue arranges the elements according to their natural order. The elements must implement the interface java.util.Comparable or a java.util.Comparator must be provided for the constructor. For example

   PriorityQueue pq = new PriorityQueue();
   pq.add("foobar");
   pq.add("tra");
   pq.add("priorityqueue");
   pq.add("et cetera");
   
   String polled;
   while ((polled = pq.poll()) != null)
     System.out.println(polled);
 
would produce
  et cetera
  foobar
  priorityqueue
  tra
 
With Strings it is worth noting that the sorting is case sensitive so that Strings starting with capital letters take precedence.

See Also:
PriorityQueue, Serialized Form

Constructor Summary
PriorityQueue()
          Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).
PriorityQueue(Collection<? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(int initialCapacity)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
          Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue(PriorityQueue<? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(SortedSet<? extends E> c)
          Creates a PriorityQueue containing the elements in the specified collection.
 
Method Summary
 boolean add(E o)
           
 void clear()
           
 Comparator<? super E> comparator()
           
 Iterator<E> iterator()
           
 E poll()
           
 boolean remove(Object o)
           
 int size()
           
 
Methods inherited from class java.util.PriorityQueue
offer, peek
 
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()
Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering (using Comparable).


PriorityQueue

public PriorityQueue(int initialCapacity)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering (using Comparable).

Parameters:
initialCapacity - the initial capacity for this priority queue.
Throws:
IllegalArgumentException - if initialCapacity is less than 1

PriorityQueue

public PriorityQueue(int initialCapacity,
                     Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

Parameters:
initialCapacity - the initial capacity for this priority queue.
comparator - the comparator used to order this priority queue. If null then the order depends on the elements' natural ordering.
Throws:
IllegalArgumentException - if initialCapacity is less than 1

PriorityQueue

public PriorityQueue(Collection<? extends E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. If the specified collection is an instance of a java.util.SortedSet or is another PriorityQueue, the priority queue will be sorted according to the same comparator, or according to its elements' natural order if the collection is sorted according to its elements' natural order. Otherwise, the priority queue is ordered according to its elements' natural order.

Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null

PriorityQueue

public PriorityQueue(PriorityQueue<? extends E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. This priority queue will be sorted according to the same comparator as the given collection, or according to its elements' natural order if the collection is sorted according to its elements' natural order.

Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null

PriorityQueue

public PriorityQueue(SortedSet<? extends E> c)
Creates a PriorityQueue containing the elements in the specified collection. The priority queue has an initial capacity of 110% of the size of the specified collection or 1 if the collection is empty. This priority queue will be sorted according to the same comparator as the given collection, or according to its elements' natural order if the collection is sorted according to its elements' natural order.

Parameters:
c - the collection whose elements are to be placed into this priority queue.
Throws:
ClassCastException - if elements of the specified collection cannot be compared to one another according to the priority queue's ordering.
NullPointerException - if c or any element within it is null
Method Detail

add

public boolean add(E o)
Specified by:
add in interface Collection<E>
Overrides:
add in class PriorityQueue<E>

remove

public boolean remove(Object o)
Specified by:
remove in interface Collection<E>
Overrides:
remove in class PriorityQueue<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Overrides:
iterator in class PriorityQueue<E>

size

public int size()
Specified by:
size in interface Collection<E>
Overrides:
size in class PriorityQueue<E>

clear

public void clear()
Specified by:
clear in interface Collection<E>
Overrides:
clear in class PriorityQueue<E>

poll

public E poll()
Specified by:
poll in interface Queue<E>
Overrides:
poll in class PriorityQueue<E>

comparator

public Comparator<? super E> comparator()
Overrides:
comparator in class PriorityQueue<E>