3

The Java Doc says that a Stack should preferably be created from a Deque, rather than using the quintessential Stack<>. Unfortunately, it does not emphasize why so.

Deques can also be used as LIFO (Last-In-First-Out) stacks. This interface should be used in preference to the legacy Stack class. When a deque is used as a stack, elements are pushed and popped from the beginning of the deque.

Could someone please point out why so? Similarly, are there other instances wherein we should avoid using the inbuilt Collections objects? I am a C++ developer moving onto Java, hence any such subtle pointers would be helpful.

Thanks.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
P.K.
  • 379
  • 1
  • 4
  • 16
  • 1
    Stack extends Vector. See this answer: https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Patrick Parker May 16 '18 at 00:16
  • @PatrickParker, well, I don't think that question answers my doubt. Come on, the accepted answer just says _"As for a Stack equivalent - I'd look at Deque/ArrayDeque to start with."_ Do you think it is enough to classify it as a dup? – P.K. May 16 '18 at 00:21
  • P.K. - If you read more attentively, you would notice that Stack is an extension of Vector. In other words, every thing they are saying about Vector _also_ applies to Stack. – Patrick Parker May 16 '18 at 00:23
  • @P.K. You should read all the answers on that question. – Polygnome May 16 '18 at 00:24
  • Read also this answer which discusses stacks more in depth; https://stackoverflow.com/a/35372152/7098259 – Patrick Parker May 16 '18 at 00:27
  • @PatrickParker, ah, that latest link helped a lot. Thank you. Also, are there any other `Collections` that might have this concern as well? – P.K. May 16 '18 at 00:29
  • @P.K. - Yes, Hashtable is another example (and its abstract version, Dictionary). Use HashMap instead. – Patrick Parker May 16 '18 at 00:38
  • @PatrickParker, thanks I'll be careful when using the two - a Stack and a Hashtable. I appreciate your help. – P.K. May 16 '18 at 00:39

2 Answers2

6

Stack extends Vector which means that it synchronizes for each individual operation.

Odds are you'll have one thread accessing the data structure, so synchronizing on each operation is a waste of CPU time. You'll spend all of your time grabbing and releasing the lock on the object, and very little of it actually adding or removing items.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

Java generics were added after initial implementations of collections; Stack is from Java 1.0 - and rather then break existing code when they added generics, it was decided to add classes that duplicate functionality (but provide a consistent API). That is why you should prefer a Deque - it provides an API consistent with all of the other Java Collections.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 3
    wrong, `Stack` also supports Java Generics. – Patrick Parker May 16 '18 at 00:18
  • So, just to be clear, you mean implementing a `Deque` interface instead of `Stack<>`? – P.K. May 16 '18 at 00:18
  • @PatrickParker My wording is perhaps awkward. Collections weren't added until Java (1.)2, Generics are Java 5, and Deque was added in Java 6. But `Stack` predates all of that! – Elliott Frisch May 16 '18 at 00:57
  • 5
    Generics have nothing to do with the reason, as Stack supports generics. The reason has more to do with the fact that Stack, like Vector, is inherently flawed due to how its synchronization works. They couldn't fix that as classes explicitly depending on that synchronization logic would then break. – Mark Rotteveel May 18 '18 at 15:50
  • It is correct that generics was added later. It is not correct that they were the reason the collections hierarchy was created in addition to what Java already had. – Thorbjørn Ravn Andersen Mar 13 '23 at 14:46