When Java 6 was introduced, it was Sun's recommendation to use the Deque (pronounced "deck") for all Stack and Queue implemenations, which combines both in one collection class containing the familiar Stack and Queue methods, and to deprecate the legacy Stack and Queue classes from Java 5. JavaTools implements the Stack and Queue with the Deque.

In JavaTools a stack can simply be created with JDeque[]. It then has to be filled with elements afterwards.



However, it's also possible to create a new stack and supply initial values during creation:



JDequeSize[] returns the number of elements in the stack, and JDequeIsEmpty[] shows if the stack is empty:





A new element is pushed into the stack with JDequePush[]or JDequeAdd[]:


The whole list of elements in the stack can be shown with JDequeElements[]:



The type of elements that can be put in a stack can be anything: Integer, Real, String, or even List:









JDequePeek[] retrieves, but does not remove the first element of the stack:



JStackPop[] retrieves and removes the first element of the stack:





JDequeGetFirst[] and JDequeGetLast[] show the first and last elements in the stack:





With JDequeRemoveFirst[] and JDequeRemoveLast[] we can remove the first and last elements from the stack:







With JDequeRemoveFirstOccurrence[] and JDequeRemoveLastOccurrence[] we can remove the elements of the first and last occurrences from the stack:







With JDequeContains[] we can see if the stack contains a particular element:












Note the memory-footprint the JDeque[] has on the Mathematica system: Not a single byte of data is stored in the kernel:








Compare against storing eight million integers in the Mathematica kernel:




Spikey Created with Wolfram Mathematica 7.0