Java Reference
In-Depth Information
turns a special value (false or null) in the presence of failure. The methods that return
aspecialvalueareusefulinthecontextofcapacity-restricted Queue implementations
where failure is a normal occurrence.
Note The offer() method is generally preferable to add() when using a
capacity-restricted queue because offer() does not throw IllegalStateEx-
ception .
Javasuppliesmany Queue implementationclasses,wheremostoftheseclassesare
members of the java.util.concurrent package: LinkedBlockingQueue ,
LinkedTransferQueue ,and SynchronousQueue areexamples.Incontrast,the
java.util package provides LinkedList and PriorityQueue as its Queue
implementation classes.
Caution Many Queue implementationclassesdonotallownullelementstobead-
ded.However,someclasses(suchas LinkedList )permitnullelements.Youshould
avoid adding a null element because null is used as a special return value by the
peek() and poll() methods to indicate that a queue is empty.
PriorityQueue
The PriorityQueue classprovidesanimplementationofa priority queue ,whichis
aqueuethatordersitselementsaccordingtotheirnaturalorderingorbyacomparator
provided when the queue is instantiated. Priority queues do not permit null elements,
and do not permit insertion of non- Comparable objects when relying on natural or-
dering.
Theelementattheheadofthepriorityqueueistheleastelementwithrespecttothe
specifiedordering.Ifmultipleelementsaretiedforleastelement,oneofthoseelements
isarbitrarilychosenastheleastelement.Similarly,theelementatthetailofthepriority
queue is the greatest element, which is arbitrarily chosen when there is a tie.
Priorityqueuesareunbounded,buthaveacapacitythatgovernsthesizeoftheintern-
alarraythatisusedtostorethepriorityqueue'selements.Thecapacityvalueisatleast
aslargeasthequeue'slength,andgrowsautomaticallyaselementsareaddedtothepri-
ority queue.
PriorityQueue (whosegenerictypeis PriorityQueue<E> )suppliessixcon-
structors:
Search WWH ::




Custom Search