Collection Framework - Stack class

Stack is again a legacy class. It is a impelementation of LIFO concept.

It is a child class of Vector. What that means is it has all the features of a vector, in addition to that, it also defines its own methods for pushing and popping the elements.




While storing it follows the index mechanism, but for searching it follows the offset as shown in the above figure. E.g. A is stored at 0 index, but if you search it will return 3 reason being you need to pop 3 times to get A out of stack.

As vector, this was also reimplemented to include generics and implement the List interface.

Stack comes with a default constructor only :

Stack() and Stack<E>()

The example for Stack class can be found here

It shows the implementation of both list specific methods as well as push(), pop(), peek() methods along with Enumeration.

Comments

Popular posts from this blog

Collection Framework - HashSet And LinkedHashSet class

Collection Framework - Cursors in Java

Hashed data structures