|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection<E> edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractQueue<E> edu.cmu.cs.pattis.cs151xx.orderedCollections.LinkedQueue<E>
public class LinkedQueue<E>
This class implements the OrderedCollection
interface
with FIFO behavior: first in/first out.
It is backed by a linked list.
It makes a guarantee that the iteration order is FIFO.
This class permits the null
element.
This class offers constant time performance for the basic operations
(add
, remove
, peekNext
, and
size
).
Iterating over this set requires time proportional to the size of the
queue.
Note that this implementation is not synchronized. If multiple threads access a queue concurrently, and at least one of the threads modifies the queue, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the queue.
The iterators returned by this class's iterator method are fail-fast:
if the queue is modified at any time after the iterator is created,
in any way, the Iterator throws a
ConcurrentModificationException
.
Thus, in the face of concurrent modification, the iterator fails
quickly and cleanly, rather than risking arbitrary,
non-deterministic behavior at an undetermined time in the future.
Constructor Summary | |
---|---|
LinkedQueue()
Constructs a new, empty queue. |
|
LinkedQueue(java.util.Collection<E> c)
Constructs a new queue containing the elements in the specified collection. |
|
LinkedQueue(E[] o)
Constructs a new queue containing the elements in the specified array. |
|
LinkedQueue(OrderedCollection<E> o)
Constructs a new queue containing the elements in the specified ordered collection. |
Method Summary | |
---|---|
boolean |
add(E o)
Ensures that this queue contains the specified element. |
void |
clear()
Removes all of the elements from this queue. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this queue. |
E |
peek()
Returns (without removing) the next elements in this queue, according to FIFO behavior. |
E |
remove()
Returns/removes the next element in this queue, according to FIFO behavior. |
java.lang.String |
toString()
Returns a string representation of this queue. |
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractQueue |
---|
equals |
Methods inherited from class edu.cmu.cs.pattis.cs151xx.orderedCollections.AbstractOrderedCollection |
---|
addAll, hashCode, isEmpty, size, toArray, toArray, toCollection |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public LinkedQueue()
public LinkedQueue(OrderedCollection<E> o)
o
- - the ordered collection whose elements are to be placed
into this queue.public LinkedQueue(java.util.Collection<E> c)
c
- - the collection whose elements are to be placed into this
queue.public LinkedQueue(E[] o)
o
- - the array whose elements are to be placed into this
queue.Method Detail |
---|
public void clear()
AbstractOrderedCollection
and is more efficient.
clear
in interface OrderedCollection<E>
clear
in class AbstractOrderedCollection<E>
public boolean add(E o)
true
if this collection changed as a result of the
call.
This always happens because queues allow duplicates.
add
in interface OrderedCollection<E>
add
in class AbstractOrderedCollection<E>
o
- - the element to store in the queue
true
if this queue collection changed as a
result of the call.public E remove() throws java.util.NoSuchElementException
remove
in interface OrderedCollection<E>
remove
in class AbstractOrderedCollection<E>
java.util.NoSuchElementException
- if the queue is emptypublic E peek() throws java.util.NoSuchElementException
peek
in interface OrderedCollection<E>
peek
in class AbstractOrderedCollection<E>
java.util.NoSuchElementException
- if the queue is emptypublic java.util.Iterator<E> iterator()
iterator
over the elements in this queue.
The ordered is guaranteed to exhibit FIFO behavior.
iterator
in interface OrderedCollection<E>
iterator
in interface java.lang.Iterable<E>
iterator
in class AbstractOrderedCollection<E>
iterator
over the elements in this queuepublic java.lang.String toString()
This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.
toString
in class AbstractOrderedCollection<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |