public class IlvUnsynchronizedStack<E> extends ArrayList<E> implements Queue<E>
Queue
interface and allow the list to be treated as a
stack. Because it is based on ArrayList
, this class is unsynchronized.
Therefore, it gives better performance than the standard JDK Stack
class.
The offer(E)
method is identical to ArrayList.add(Object)
in that it
pushes an item onto the stack and always returns true
. A
push method is also provided that mimics the JDK's
Stack
class. Although ArrayList
allows insertion of
null items, you should avoid inserting them into an
IlvUnsynchronizedStack because null is also used as a special return
value by the poll()
method to indicate that the stack contains no elements.
The poll()
, remove()
, and pop()
methods remove and
return the item from the top of the stack. The remove
and pop
methods differ only in the type of exceptions that they throw when the stack is empty.
The poll
method differs from the other two in that it returns
null
when the stack is empty. The element()
and peek()
methods return, but do not remove, the item at the top of the stack.
modCount
Constructor and Description |
---|
IlvUnsynchronizedStack()
Constructs an empty stack with an initial capacity of ten.
|
IlvUnsynchronizedStack(int initialCapacity)
Constructs an empty stack with the specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
Returns a shallow copy of this IlvUnsynchronizedStack instance.
|
E |
element()
Returns the object at the top of the stack without removing it from the stack.
|
boolean |
empty()
Returns whether this stack is empty.
|
boolean |
offer(E item)
Pushes an item onto the top of the stack by adding it to the end of the list.
|
E |
peek()
Returns the object at the top of the stack without removing it from the stack.
|
E |
poll()
Removes the object at the top of the stack and returns that item as the value of
this method.
|
E |
pop()
Removes the object at the top of the stack and returns that item as the value of
this method.
|
E |
push(E item)
Pushes an item onto the top of the stack.
|
E |
remove()
Removes the object at the top of the stack and returns that item as the value of
this method.
|
add, add, addAll, addAll, clear, contains, ensureCapacity, forEach, get, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSize
equals, hashCode
containsAll, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
containsAll, equals, hashCode
public IlvUnsynchronizedStack(int initialCapacity)
initialCapacity
- The initial capacity of the stack.IllegalArgumentException
- if the specified initial capacity
is negativepublic IlvUnsynchronizedStack()
public Object clone()
public boolean offer(E item)
Although this class does not prohibit the insertion of null items,
you should avoid inserting them because because null is also used as a
special return value by the poll()
method to indicate that the stack contains
no elements.
offer
in interface Queue<E>
item
- The item to push onto the stack.push(E)
,
ArrayList.add(Object)
public E poll()
public E remove()
poll()
method in that it throws an
exception instead of returning null
if the stack is empty. This method
differs from the pop()
method in that it throws a NoSuchElementException
instead of an EmptyStackException
if the stack is empty.public E peek()
element()
and java.util.Stack.peek()
methods in that it returns null
instead of throwing an exception if the
stack is empty.public E element()
peek()
method in that it throws an exception instead of
returning null
if the stack is empty.element
in interface Queue<E>
NoSuchElementException
- if the stack is empty.peek()
public E push(E item)
Although this class does not prohibit the insertion of null items,
you should avoid inserting them because because null is also used as a
special return value by the poll()
method to indicate that the stack contains
no elements.
item
- The item to be pushed onto this stack.item
argument.offer(E)
,
ArrayList.add(Object)
public E pop()
poll()
method in that it throws an
exception instead of returning null
if the stack is empty. This method
differs from the remove()
method in that it throws an EmptyStackException
instead of a NoSuchElementException
if the stack is empty.public boolean empty()
© Copyright Rogue Wave Software, Inc. 1997, 2018. All Rights Reserved.