Java Reference
In-Depth Information
public T remove()
Retrieves and removes the entry at the front of this priority queue, but throws
NoSuchElementException if the priority queue is empty prior to the operation.
public T poll()
Retrieves and removes the entry at the front of this priority queue, but returns null if the
priority queue is empty prior to the operation.
public T element()
Retrieves the entry at the front of this priority queue, but throws NoSuchElementException
if the priority queue is empty.
public T peek()
Retrieves the entry at the front of this priority queue, but returns null if the priority queue is empty.
public boolean isEmpty()
Detects whether this priority queue is empty.
public void clear()
Removes all entries from this priority queue.
public int size()
Gets the number of elements currently in this priority queue.
Instances of PriorityQueue grow in size as needed by a client.
C HAPTER S UMMARY
The ADT queue organizes its entries on a first-in, first-out basis. Among its items, the one added first, or
earliest, is at the front of the queue, and the one added most recently is at the back of the queue.
A queue's major operations— enqueue , dequeue , and getFront —deal only with the ends of the queue. The
method enqueue adds an entry to the back of the queue; dequeue removes and returns the entry at the front
of the queue, and getFront just returns it.
You can use a queue to simulate a waiting line. A time-driven simulation counts simulated units of time.
Customers arrive at random times, are assigned a random transaction time, and enter a queue.
When computing the capital gain from a sale of stock, you must sell shares in the order in which you
purchased them. If you record your purchases of individual shares in a queue, they will be in the order in
which they must be sold.
A double-ended queue, or deque, has operations that add, remove, or retrieve entries at both its front and
back. As such, it combines and expands the operations of a queue and a stack. The deque's major operations
are addToFront , removeFront , getFront , addToBack , removeBack , and getBack .
A priority queue organizes its entries according to their priorities, as determined by the entries' compareTo
method. Besides adding entries to a priority queue, you can retrieve and remove the entry with the highest
priority.
A mutable object has set methods; an immutable object does not.
P ROGRAMMING T IP
Methods such as getFront and dequeue must behave reasonably when a queue is empty. Here, we specify
that they return null . Another possibility is to have them throw an exception.
 
 
Search WWH ::




Custom Search