What is the best class for stack behavior in Java

I need to use a simple stack in my code (simple push and pop). Although a trivial situation, after some, we realized that I really do not know which class Java (Collection framework?) Is best suited for this behavior. I know that there are some classes of the framework that also have behavior in the form of a stack, as well java.util.Stack- but seeing that it expands Vectorand does not remember how this class is mentioned in any Java assembly tutorials / tutorials that I read (even for SCJP certification) I feel that this may not be the best choice, although simple online tutorials use this class.

What class should I work with?

Requirements: almost none. Just click and pop. No concurrent access. Holds Stringin the amount of 20 - several hundred (worst case).

+3
source share
1 answer

Use Deque. It is also mentioned in the Stack class documentation. (according to @RohitJain)

LinkedList works too, these specifications you are talking about happen in linear time with this data structure.

The truth is that for something so simple, there are several essentially equally good options. If you plan to expand it, tell us what else you can do in future iterations of the program, and this may be a good way to decide which one.

+3
source

All Articles