T
- component typepublic interface Stack<T>
Stack
stores elements allowing a last-in-first-out (LIFO) retrieval.
List
extends Stack
, so use one of List
's static factory methods to obtain a Stack
.
Stack API:
See Okasaki, Chris: Purely Functional Data Structures (p. 7 ff.). Cambridge, 2003.Modifier and Type | Method and Description |
---|---|
boolean |
isEmpty()
Checks if this Stack is empty.
|
static <T> Stack<T> |
narrow(Stack<? extends T> stack)
Narrows a widened
Stack<? extends T> to Stack<T>
by performing a type safe-cast. |
T |
peek()
Returns the head element without modifying the Stack.
|
Option<T> |
peekOption()
Returns the head element without modifying the Stack.
|
Stack<T> |
pop()
Removes the head element from this Stack.
|
Tuple2<T,? extends Stack<T>> |
pop2()
Removes the head element from this Stack.
|
Option<? extends Tuple2<T,? extends Stack<T>>> |
pop2Option()
Removes the head element from this Stack.
|
Option<? extends Stack<T>> |
popOption()
Removes the head element from this Stack.
|
Stack<T> |
push(T... elements)
Pushes the given elements on top of this Stack.
|
Stack<T> |
push(T element)
Pushes a new element on top of this Stack.
|
Stack<T> |
pushAll(Iterable<T> elements)
Pushes the given elements on top of this Stack.
|
static <T> Stack<T> narrow(Stack<? extends T> stack)
Stack<? extends T>
to Stack<T>
by performing a type safe-cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Stack
.stack
- A Stack
.stack
instance as narrowed type Stack<T>
.boolean isEmpty()
T peek()
NoSuchElementException
- if this Stack is emptyOption<T> peekOption()
None
if this Stack is empty, otherwise a Some
containing the head elementStack<T> pop()
NoSuchElementException
- if this Stack is emptyOption<? extends Stack<T>> popOption()
None
if this Stack is empty, otherwise a Some
containing the elements of this Stack without the head elementTuple2<T,? extends Stack<T>> pop2()
NoSuchElementException
- if this Stack is emptyOption<? extends Tuple2<T,? extends Stack<T>>> pop2Option()
None
if this Stack is empty, otherwise Some
Tuple
containing the head element and the remaining elements of this StackStack<T> push(T element)
element
- The new elementStack
instance, containing the new element on top of this StackStack<T> push(T... elements)
elements
- Elements, may be emptyStack
instance, containing the new elements on top of this StackNullPointerException
- if elements is nullStack<T> pushAll(Iterable<T> elements)
elements
- An Iterable of elements, may be emptyStack
instance, containing the new elements on top of this StackNullPointerException
- if elements is nullCopyright © 2016. All Rights Reserved.