I need to use simple stack in my code (simple push & pop). Although trivial situation, after some thought I realized that I in fact don't know which Java (Collection framework?) class is best suited for this behavior. I know there are some collection framework classes which have also stack like behavior and also java.util.Stack - but seeing it extends Vector and not remembering this class being mentioned in any Java collection tutorials /textbooks I read (not even for SCJP certification) I have feeling this might not be the best choice, even though simple tutorials online use this class.
What class should I go with?
Requirements: Almost none. Just push and pop. No concurrent access. Holds String in count 20 - few hundreds (worse case).
Use a Deque. It's also mentioned in the documentation of Stack class. (as per @RohitJain)
LinkedList works too—those specs you're talking about all occur in linear time with that data structure.
The truth is that for something so simple, there are several essentially equally good choices. If you're thinking about expanding it, then tell us what else you might want to do in future iterations of the program, and that might be a good way to decide which one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With