public class ArrayQueue<E>
extends java.lang.Object
This array-based implementation is optimized for performance. Entries
can be added/removed to/from the queue in amortized constant time.
The cost of adding/removing N entries to/from the queue is O(N), and
the constant factor is less than that for an implementation based on
LinkedList
.
For queues that contain at least a small number of entries, the length
of the array used to implement the queue is less than twice the maximum
number of entries in the queue. Furthermore, this length is always less
than three times the number of entries in the queue.
Therefore, this implementation requires less memory than one based on
LinkedList
.
Constructor and Description |
---|
ArrayQueue()
Constructs a queue with default capacity.
|
ArrayQueue(int capacity)
Constructs a queue with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E e)
Adds the specified entry to the back of the queue.
|
void |
ensureCapacity(int capacity)
Ensures that the capacity of the queue is not less than the
specified value.
|
E |
first()
Returns (but does not remove) the entry from the front of the queue.
|
boolean |
isEmpty()
Determines whether the queue is empty.
|
E |
remove()
Removes and returns the entry from the front of the queue.
|
int |
size()
Returns the number of entries in the queue.
|
void |
trimToSize()
Sets the capacity of the queue equal to its current size.
|
public ArrayQueue()
public ArrayQueue(int capacity)
This constructor may be used to reduce the cost of adding a large number of entries to the queue, when that number of entries is known in advance.
capacity
- the initial capacity.public void add(E e)
e
- the entry.public E first()
public E remove()
public boolean isEmpty()
public void ensureCapacity(int capacity)
This method may be used to reduce the cost of adding a large number of entries to the queue, when that number of entries is known in advance.
capacity
- the capacity.public int size()
public void trimToSize()