Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Stack appropriate data structure?

Hmm. I'm noticing Stack is a subclass of Vector, and I thought Vector and Hashtable were considered "old" datastructures because of their builtin synchronization even if you don't need it. (vs. List, Map, etc. which don't provide it for you)

That and it's a class, not an interface.

Is there a more modern, recommended alternative?

like image 981
Jason S Avatar asked Aug 24 '09 02:08

Jason S


2 Answers

java.util.Deque

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.

like image 73
John Kugelman Avatar answered Nov 16 '22 00:11

John Kugelman


I ended up using LinkedList for my purposes (add() and removeLast() being push and pop operations). Oops, looks like this is a duplicate Q.

like image 37
Jason S Avatar answered Nov 16 '22 02:11

Jason S