Interface PStack<E>
- Type Parameters:
E- the type of elements maintained by this stack
- All Superinterfaces:
Iterable<E>
Persistent (functional) Stack.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanTest given predicate on elements and return true if any of elements matches the predicatebooleanisEmpty()static <E> PStack<E>of()Returns a persistent stack containing zero elements.static <E> PStack<E>of(E e1) Returns a persistent stack containing one element.static <E> PStack<E>of(E e1, E e2) Returns a persistent stack containing two elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3) Returns a persistent stack containing three elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4) Returns a persistent stack containing four elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5) Returns a persistent stack containing five elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5, E e6) Returns a persistent stack containing six elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) Returns a persistent stack containing seven elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) Returns a persistent stack containing eight elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) Returns a persistent stack containing nine elements, pushed left to right.static <E> PStack<E>of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) Returns a persistent stack containing ten elements, pushed left to right.peek()peek(int i) pop()intsize()Naive implementation has O(n) time complexity, where n is number of elements.stream()toString()Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
push
- Returns:
- new stack with added element
-
peek
E peek()- Returns:
- element at the top of this stack
- Throws:
IllegalStateException- if this stack is empty.
-
peek
- Parameters:
i- - index of element to be returned, 0 means top of the stack- Returns:
- i-th element from top of the stack
- Throws:
IllegalStateException- if stack has less than i elements
-
pop
- Returns:
- new stack with removed element
- Throws:
IllegalStateException- if this stack is empty.
-
isEmpty
boolean isEmpty()- Returns:
- true if this stack contains no elements
-
anyMatch
Test given predicate on elements and return true if any of elements matches the predicate- Parameters:
predicate- predicate to be tested- Returns:
- true if any of the stack elements satisfies the predicate
-
size
int size()Naive implementation has O(n) time complexity, where n is number of elements. More clever implementation could take advantage of PStack's immutability- Returns:
- number of elements in the stack
-
toString
String toString() -
stream
- Returns:
- stream of the stack's elements, starting at the top of the stack
-
of
Returns a persistent stack containing zero elements.- Type Parameters:
E- thePStack's element type- Returns:
- an empty
PStack
-
of
Returns a persistent stack containing one element.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the single element- Returns:
- a
PStackcontaining the specified element
-
of
Returns a persistent stack containing two elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing three elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing four elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing five elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing six elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth elemente6- the sixth element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing seven elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth elemente6- the sixth elemente7- the seventh element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing eight elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth elemente6- the sixth elemente7- the seventh elemente8- the eighth element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing nine elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth elemente6- the sixth elemente7- the seventh elemente8- the eighth elemente9- the ninth element- Returns:
- a
PStackcontaining the specified elements
-
of
Returns a persistent stack containing ten elements, pushed left to right.- Type Parameters:
E- thePStack's element type- Parameters:
e1- the first elemente2- the second elemente3- the third elemente4- the fourth elemente5- the fifth elemente6- the sixth elemente7- the seventh elemente8- the eighth elemente9- the ninth elemente10- the tenth element- Returns:
- a
PStackcontaining the specified elements
-