Class Stack<E>

  • Type Parameters:
    E - the type of elements stored in the map

    public class Stack<E>
    extends java.lang.Object
    A simple, streamlined implementation of Stack. The implementation is not threadsafe.
    See Also:
    CollectionFactory.newStack()
    • Constructor Summary

      Constructors 
      Constructor Description
      Stack()
      Normal constructor supporting an initial size of 20.
      Stack​(int initialSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the stack, the same as popping off all elements.
      int getDepth()
      Returns the number of items currently in the stack.
      java.lang.Object[] getSnapshot()
      Returns a snapshot of the current state of the stack as an array of objects.
      boolean isEmpty()
      Returns true if the stack is empty.
      E peek()
      Returns the top element of the stack without affecting the stack.
      E pop()
      Pops the top element off the stack and returns it.
      void push​(E item)
      Pushes a new item onto the stack.
      java.lang.String toString()
      Describes the stack, listing the element in order of depth (top element first).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Stack

        public Stack()
        Normal constructor supporting an initial size of 20.
      • Stack

        public Stack​(int initialSize)
        Parameters:
        initialSize - the initial size of the internal array (which will be expanded as necessary). For best efficiency, set this to the maximum depth of the stack.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns true if the stack is empty.
      • getDepth

        public int getDepth()
        Returns the number of items currently in the stack.
      • clear

        public void clear()
        Clears the stack, the same as popping off all elements.
      • push

        public void push​(E item)
        Pushes a new item onto the stack.
      • pop

        public E pop()
        Pops the top element off the stack and returns it.
        Returns:
        the top element of the stack
        Throws:
        java.lang.IllegalStateException - if the stack is empty
      • peek

        public E peek()
        Returns the top element of the stack without affecting the stack.
        Returns:
        top element on the stack
        Throws:
        java.lang.IllegalStateException - if the stack is empty
      • toString

        public java.lang.String toString()
        Describes the stack, listing the element in order of depth (top element first).
        Overrides:
        toString in class java.lang.Object
        Returns:
        string description of the stack
      • getSnapshot

        public java.lang.Object[] getSnapshot()
        Returns a snapshot of the current state of the stack as an array of objects. The first object is the deepest in the stack, the last object is the most shallowest (most recently pushed onto the stack). The returned array may be manipulated (it is a copy).
        Returns:
        the stack as an object array