I want to have LIFO effect and I want it to be synchronized. Does anyone know which one of these two implementations I should use? Been googling for a while, still no good answer.
Bottom line: what are differences, why use one over another, why is it said to favor arrayDequeue?
From question:
It is not said to favor
ArrayDeque(a class).It is said that you should favor
Deque(an interface) overStack(a class), because you should program to an interface, allowing you to substitute the implementation without otherwise changing your code.The "it is said ..." is right there in the javadoc of
Stack:The Java Runtime Library comes with the following choices of implementation for a
Deque:ArrayDeque- generally best choice for single-threaded useConcurrentLinkedDeque- generally best choice for multi-threaded useLinkedBlockingDeque- if you need stack with size limitLinkedList- if stack can grow big, and you want to reclaim space as it shrinksLinkedBlockingDequeuses locks, which is similar to usingsynthronized, but none of the others usesynchronized. The wayConcurrentLinkedDequeis implemented to be thread-safe has proven to perform better than an implementation usingsynchronized.ArrayDequeis faster thanStackbecause it is not usingsynchronized, so is better for non-thread-safe code.See also: Why should I use Deque over Stack?
See also: Why is Java Vector (and Stack) class considered obsolete or deprecated?