|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractQueue<E>
util.SortedQueue<E>
E
- the type of elements held in this collectionpublic class SortedQueue<E>
An unbounded sorted queue able to update its elements.
This queue grants the correct order also when the elements change their ordering.
The elements of the sorted queue are ordered according to their natural ordering,
or by a Comparator provided at queue construction time,
depending on which constructor is used.
A sorted queue does not permit null elements.
A sorted queue relying on natural ordering also
does not permit insertion of non-comparable objects (doing so may result in ClassCastException).
The head of this queue is the smallest element with respect to the specified ordering.
If there are more smallest elements the first inserted will be the head of the queue.
The queue retrieval operations poll, remove
and peek
search the howl queue to find the current smallest element.
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.
The Iterator provided in method iterator() is guaranteed to traverse the elements
of the sorted queue in the insertion order not the imposed natural ordering.
If you need ordered traversal, consider using Arrays.sort(pq.toArray()).
Note that this implementation is not synchronized.
Multiple threads should not access a SortedQueue instance concurrently
if any of the threads modifies the queue.
Implementation note: this implementation provides
constant time for enqueing methods (offer and add) and testing methods (size and isEmpty)
and linear time for the retrieval methods (poll, remove, peek, element and contains);
This class is an extension to the Java Collections Framework.
Constructor Summary | |
---|---|
SortedQueue()
Creates a SortedQueue that orders its elements according to their natural ordering. |
|
SortedQueue(java.util.Collection<E> c)
Creates a SortedQueue containing the elements in the specified collection. |
|
SortedQueue(java.util.Comparator<? super E> comparator)
Creates a SortedQueue with the specified initial capacity that orders its elements according to the specified comparator. |
|
SortedQueue(java.util.PriorityQueue<E> p)
Creates a SortedQueue containing the elements in the specified priority queue. |
|
SortedQueue(SortedQueue<E> s)
Creates a SortedQueue containing the elements in the specified sorted queue. |
|
SortedQueue(java.util.SortedSet<E> s)
Creates a SortedQueue containing the elements in the specified sorted set. |
Method Summary | |
---|---|
void |
clear()
|
java.util.Comparator<? super E> |
comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements. |
boolean |
isEmpty()
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements of this queue. |
boolean |
offer(E value)
Inserts the specified element into this priority queue. |
E |
peek()
Returns but retains the smallest element in the queue. |
E |
poll()
Returns and removes the smallest element in the queue. |
int |
size()
|
Methods inherited from class java.util.AbstractQueue |
---|
add, addAll, element, remove |
Methods inherited from class java.util.AbstractCollection |
---|
contains, containsAll, remove, 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, remove, removeAll, retainAll, toArray, toArray |
Constructor Detail |
---|
public SortedQueue()
public SortedQueue(java.util.Comparator<? super E> comparator)
comparator
- the comparator that will be used to order this sorted queue.
If null, the natural ordering of the elements will be usedpublic SortedQueue(java.util.Collection<E> c)
c
- the collection whose elements are to be placed into this priority queue
java.lang.ClassCastException
- if elements of the specified collection cannot be compared to one another according to the sorted queue's ordering
java.lang.NullPointerException
- if the specified collection or any of its elements are nullpublic SortedQueue(java.util.SortedSet<E> s)
s
- the sorted set whose elements are to be placed into this sorted queue
java.lang.ClassCastException
- if elements of c cannot be compared to one another according to sorted set's ordering
java.lang.NullPointerException
- if the specified sorted set or any of its elements are nullpublic SortedQueue(java.util.PriorityQueue<E> p)
p
- the priority queue whose elements are to be placed into this sorted queue
java.lang.ClassCastException
- if elements of c cannot be compared to one another according to p's ordering
java.lang.NullPointerException
- if the specified priority queue or any of its elements are nullpublic SortedQueue(SortedQueue<E> s)
s
- the sorted queue whose elements are to be placed into this sorted queue
java.lang.ClassCastException
- if elements of c cannot be compared to one another according to sorted queue's ordering
java.lang.NullPointerException
- if the specified sorted queue or any of its elements are nullMethod Detail |
---|
public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
iterator
in interface java.util.Collection<E>
iterator
in class java.util.AbstractCollection<E>
AbstractCollection.iterator()
public int size()
size
in interface java.util.Collection<E>
size
in class java.util.AbstractCollection<E>
AbstractCollection.size()
public boolean offer(E value)
java.lang.ClassCastException
- if the given element is not comparableQueue.offer(java.lang.Object)
public E peek()
Queue.peek()
public E poll()
Queue.poll()
public void clear()
clear
in interface java.util.Collection<E>
clear
in class java.util.AbstractQueue<E>
AbstractQueue.clear()
public boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
isEmpty
in class java.util.AbstractCollection<E>
AbstractCollection.isEmpty()
public java.util.Comparator<? super E> comparator()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |