1

As we know that stack is a data structure which follows LIFO order. And as per my knowledge stack allows to access top element only. But in Java we can use iterator with stack which somehow contradict the above idea of allowing access to only top element.

Sample code

    Stack<Integer> s=new Stack<Integer>();
    s.push(10);
    s.push(20);
    s.push(30);
    ------------------------------------------------------------------------
    Iterator<Integer> itr=s.iterator();
    while(itr.hasNext())
    {
      System.out.print(itr.next() + " ");    // ouput:- 10 20 30
    }

Now I have an iterator on stack which can traverse the stack in FIFO order and I can even delete any element by using itr.remove()

It's very confusing :)

Please explain...

PS: In case I use listIterator at the place of iterator then it's even more confusing because former one provides more methods.

  • 2
    "Top element only" is definitely wrong. Stacks typically allow you to access elements relative to the top: top-1, top-2, etc. The iterator just allows you to go through all the elements in the same way. – markspace Oct 09 '16 at 18:16
  • 2
    You should probably avoid using stack as its backed by vector where very method is thread safe. – SMA Oct 09 '16 at 18:18
  • 1
    this goes slightly against the 'pure' theoretical stack, and most times you won't use this (except to print and debug your stack), but it is convenient :) You can see the definition of stack as 'it has push, pop, top' as opposed to 'it has ONLY push, pop, top' – okaram Oct 09 '16 at 18:19
  • @markspace here what I mean by "top element only" is the typical theory behind stack and LIFO. – Haridesh Yadav Oct 09 '16 at 18:23
  • 1
    Possible duplicate of [What are the negative aspects of Java class Stack inheriting from Vector?](http://stackoverflow.com/questions/2922257/what-are-the-negative-aspects-of-java-class-stack-inheriting-from-vector) – user140547 Oct 09 '16 at 18:23
  • @SMA, You mentioned a very good point but can suggest me alternatives in java to serve the same purpose as stack does ? – Haridesh Yadav Oct 09 '16 at 18:24
  • 2
    You're looking for `Deque`, as explained in the documentation for `Stack`. – chrylis -cautiouslyoptimistic- Oct 09 '16 at 18:25

1 Answers1

3

Java Stack<T> provides more operations than the abstract stack data type. Most likely, this is done for historical reasons, because Stack has been part of Java 1.0, and its designers decided to derive it from Vector. This is how operations from Vector became operations on Stack. Once you allow additional public operations to a public class, there is no way to take them back.

This does not mean, however, that the class is somehow broken: if you do not want the additional operations provided by Vector<T>, do not use them in your code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    The same would be true if switching to [`Deque`](http://docs.oracle.com/javase/8/docs/api/java/util/Deque.html), as recommended by the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/util/Stack.html) of `Stack`. If `Stack` hadn't already been a class, they would very likely have added a `Stack` interface with just the `push()`, `pop()`, and `peek()` methods. – Andreas Oct 09 '16 at 18:53