| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
PriorityQueue priorityQueue1 = new PriorityQueue();
Based on 7 examples
public class PriorityQueue extends AbstractQueue implements Serializable
An unbounded priority {@linkplain Queue queue} based on a priority heap. The elements of the priority queue are ordered according to their {@linkplain Comparable natural ordering}, or by a {@link Comparator} provided at queue construction time, depending on which constructor is used. A priority queue does not permit {@code null} elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in {@code ClassCastException}).
The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily. The queue retrieval operations {@code poll}, {@code remove}, {@code peek}, and {@code element} access the element at the head of the queue.
A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.
This class and its iterator implement all of the optional methods of the {@link Collection} and {@link Iterator} interfaces. The Iterator provided in method {@link #iterator()} is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using {@code Arrays.sort(pq.toArray())}.
Note that this implementation is not synchronized. Multiple threads should not access a {@code PriorityQueue} instance concurrently if any of the threads modifies the queue. Instead, use the thread-safe {@link java.util.concurrent.PriorityBlockingQueue} class.
Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods ({@code offer}, {@code poll}, {@code remove()} and {@code add}); linear time for the {@code remove(Object)} and {@code contains(Object)} methods; and constant time for the retrieval methods ({@code peek}, {@code element}, and {@code size}).
This class is a member of the Java Collections Framework.
Constructor Summary | |
---|---|
Creates a with the default initial capacity (11) that orders its elements according to their java.lang.Comparable. |
|
Creates a containing the elements in the specified collection. |
|
PriorityQueue(int initialCapacity) Creates a with the specified initial capacity that orders its elements according to their java.lang.Comparable. |
|
PriorityQueue(int initialCapacity, Comparator comparator) Creates a with the specified initial capacity that orders its elements according to the specified comparator. |
|
Creates a containing the elements in the specified priority queue. |
|
Creates a containing the elements in the specified sorted set. |
Method Summary | |
---|---|
boolean |
Inserts the specified element into this priority queue. |
void |
clear() Removes all of the elements from this priority queue. |
Comparator |
Returns the comparator used to order the elements in this queue, or if this queue is sorted according to the java.lang.Comparable of its elements. |
boolean |
Returns if this queue contains the specified element. |
Iterator |
iterator() Returns an iterator over the elements in this queue. |
boolean |
Inserts the specified element into this priority queue. |
Object |
peek() |
Object |
poll() |
boolean |
Removes a single instance of the specified element from this queue, if it is present. |
int |
size() |
Object[] |
toArray() Returns an array containing all of the elements in this queue. |
Object[] |
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. |
Methods inherited from class java.util.AbstractQueue |
---|
add, addAll, clear, element, remove |
Methods inherited from class java.util.AbstractCollection |
---|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public PriorityQueue()
public PriorityQueue(Collection c)
c
- the collection whose elements are to be placed
into this priority queuepublic PriorityQueue(int initialCapacity)
initialCapacity
- the initial capacity for this priority queuepublic PriorityQueue(int initialCapacity, Comparator comparator)
initialCapacity
- the initial capacity for this priority queuecomparator
- the comparator that will be used to order this
priority queue. If {@code null}, the {@linkplain Comparable
natural ordering} of the elements will be used.public PriorityQueue(PriorityQueue c)
c
- the priority queue whose elements are to be placed
into this priority queuepublic PriorityQueue(SortedSet c)
c
- the sorted set whose elements are to be placed
into this priority queueMethod Detail |
---|
public boolean add(Object e)
add
in class AbstractQueue
e
public void clear()
clear
in class AbstractQueue
public Comparator comparator()
public boolean contains(Object o)
contains
in class AbstractCollection
o
- object to be checked for containment in this queuepublic Iterator iterator()
iterator
in class AbstractCollection
public boolean offer(Object e)
e
public Object peek()
public Object poll()
public boolean remove(Object o)
remove
in class AbstractCollection
o
- element to be removed from this queue, if presentpublic int size()
size
in class AbstractCollection
public Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in class AbstractCollection
public Object[] toArray(Object[] a)
If the queue fits in the specified array with room to spare (i.e., the array has more elements than the queue), the element in the array immediately following the end of the collection is set to {@code null}.
Like the {@link #toArray()} method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:
String[] y = x.toArray(new String[0]);Note that toArray(new Object[0]) is identical in function to toArray().
toArray
in class AbstractCollection
a
- the array into which the elements of the queue are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |