The Stack class in Java

Which class is better for using the stack in java and why is it better?

3 answers

Deque

For the stack, it is recommended to use interface implementations Deque. The interface includes stack methods: push, poll and peek.

Standard implementations are listed in the documentation, for example:

The choice of class depends on the logic of the application (which operations are performed and as often as the stack size changes) and will require performance evaluation.

The documentation for ArrayDeque states that as a queue, it is likely to be faster than LinkedList:

... This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

So if you don't need access from multiple threads, it makes sense to use ArrayDeque by default.

Methods poll and peek in Deque inherited from Queue and work in sequence for the queue (FIFO). To simulate the stack operation, you can use the following methods instead pollLast and peekLast.

Queue

More convenient option: using the method Collections.asLifoQueue convert Deque to an object Queue that behaves like a stack (LIFO). In this case, all methods and iterators will work correctly. Also, extra ones that are not allowed for the stack will not be available operations:

 Queue<String> stack = Collections.asLifoQueue(new ArrayDeque<>());
 stack.addAll(Arrays.asList("a", "b", "c", "d", "e"));
 //edcba
 stack.forEach(element -> System.out.println(element));

Stack is not recommended to use

There is also a standard class java.util.Stack, but it is not recommended to use it. From the documentation:

A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example: Deque<Integer> stack = new ArrayDeque<Integer>();

A more complete and consistent set of LIFO operations is provided via the Deque interface and its implementations that should be used instead of this class. For example: Deque<Integer> stack = new ArrayDeque<Integer>();

That is, the class is saved for backward compatibility, but you need to use implementations of Deque instead. This is due to historical reasons: in the first version of Java, there were errors in the implementation of Stack, for example:

  • Stack - a specific class, unlike the rest of the base collections, which are represented by interfaces: Set, List, Queue;
  • the class inherits from Vector that conceptually incorrect (redundant operations are supported).

Read more about the problems Stack:

 6
Author: default locale, 2020-06-12 12:52:24

I think you should use ArrayList or LinkedList. The second one will use more memory, but the operation of adding an element in it works fair O(1), unlike ArrayList, which has O(1) amortized. You should not use the Stack class, because in it all methods are marked as synchronized, which slows down performance if your stack uses only one thread (which is almost always the case).

 1
Author: diralik, 2017-05-23 21:40:30

There is an opinion - authoritative-from one of the authors of the red-black trees, that the built-in collections in java are very fancy. They resemble Swiss knives - they can do a lot of things. But the flip side of this is that not all operations have the same algorithmic complexity. Therefore, it is better to use collections with fewer methods, but which are well used for your task. Look at alternative stack implementations - don't stop at the standard library.

 0
Author: dSH, 2017-05-27 01:29:40