0

1) class Stack: public E push(E item)

class Stack - its push method just returns its argument (unlike newer Deque, whose respective push method is now void):

public E push(E item)

Can you provide any useful real-life / production example of using this feature?

I understand that Stack is legacy, use Deque implementations instead.

But how this feature was used in legacy apps? I guess it could be used for call chaining, but can't think of a practical example.

2) Do you often have to work with Stack class in you everyday work? Is it widely still widely used? Or most legacy apps have already been refactored to use Deque?

3) Is there any practical benefit (and is it worth it) to refactor Stack to Deque, if I encounter Stack in any legacy app which I am actively developing (adding new features)? I understand that in new apps Deque is preferrable, but if I already have Stack in legacy app, from practical point of view are there cases when it is absolutely worth effort to refactor it to Deque?

4) Am I right, that if I need use stack datastructure in multithreaded access, I shall use ConcurrentLinkedDeque (because I shall prefer Deque implementations over Stack class)? Or is there any practice to use Stack for multithreaded access nowadays? I think no, but I'd still like to ask gurus.

1 Answers1

1

1) Yes. Consider this class:

class Foo
{
    public void activate() { }
}

Sometimes it's more concise to be able to write:

Stack<Foo> s = new Stack<>();
s.push(new Foo()).activate();

Rather than:

Stack<Foo> s = new Stack<>();
Foo f = new Foo();
f.activate();
s.push(f);

2) No, I've never used it or seen it used, at least in the last 5 years. I find that Stack's reputation precedes it. Most people know to avoid it, even if they're not clear why.

3) This is a duplicate of this question: Why should I use Deque over Stack?


With regards to the question you deleted, related to the search method:

Your assumptions are incorrect. Stack extends Vector and so inherits all of Vector's public methods, one of which is elementAt(int). So you are able to use this index if you want to.

Community
  • 1
  • 1
Michael
  • 41,989
  • 11
  • 82
  • 128