Home java The Java Stack Class

The Java Stack Class

Author

Date

Category

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


Answer 1, authority 100%

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 (what operations are performed and how often, how the stack size is changed) and will require performance evaluation.

The documentation for ArrayDeque states that as a queue it will most likely 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 do not need access from multiple threads it is wise to use ArrayDeque by default.

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

Queue

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

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

Stack is not recommended

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 & lt; Integer & gt; stack = new ArrayDeque & lt; Integer & gt; ();

A more complete and consistent set of LIFO operations is provided through the Deque interface and its implementations, which should be used instead of this class. For example: Deque & lt; Integer & gt; stack = new ArrayDeque & lt; Integer & gt; ();

i.e. the class has been retained for backward compatibility, but you must use the Deque implementations instead. This is due to historical reasons: the first version of Java contained errors in the Stack implementation, for example:

  • Stack is a concrete class, unlike other base collections, which are represented by interfaces: Set , List , Queue ;
  • class inherits from Vector , which is conceptually incorrect (extra operations are supported).

Learn more about Stack issues:


Answer 2, authority 17%

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


Answer 3

There is an opinion – authoritative – from one of the authors of red-black trees that the built-in collections in java are very fancy. They resemble Swiss knives – they can do a lot. But the downside 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 particular task. Have a look at alternative stack implementations – don’t stop with the standard library.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions