Java Collections (LIFO Structure)
Solution 1
There's actually a Stack class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html
If you don't want to use that, the LinkedList class (http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html) has addFirst
and addLast
and removeFirst
and removeLast
methods, making it perfect for use as a stack or queue class.
Solution 2
I realize I'm late to the party here, but java.util.Collections (Java 7) has a static 'asLifoQueue' that takes a Deque argument and returns (obviously) a LIFO queue view of the deque. I'm not sure what version this was added.
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#asLifoQueue(java.util.Deque)
Solution 3
Deque
, ArrayDeque
, & LinkedList
While this was asked a while ago it might be wise to provide a JDK6+ answer which now provides a Deque (deck) interface which is implemented by the ArrayDeque data structure and the LinkedList was updated to implement this interface.
ConcurrentLinkedDeque
& LinkedBlockingDeque
Specialised forms for concurrent access also exist and are implemented by ConcurrentLinkedDeque and LinkedBlockingDeque.
LIFO versus FIFO
The one thing that is great about a deque is that it provides both LIFO (stack) and FIFO (queue) support it can cause confusion as to which methods are for queue operations and which are for stack operations for newcomers.
IMHO the JDK should have a Stack
interface and a Queue
interface that could still be implemented by the likes of ArrayDeque but only expose the subset of methods required for that structure, i.e. a LIFO could define pop()
, push()
and peek()
, then in the context of
LIFO<String> stack = new ArrayDeque<>();
only stack operations are exposed which stops someone accidentally calling add(E) when push(E) was intended.
Solution 4
Stack class is slowly: methods are synchronized + Stack extends synchronized Vector
Solution 5
There is a Stack class in the API. Will this meet your needs?
Related videos on Youtube
Charlie
Software engineer interested in software analysis, architecture and development. I've been mostly focused on Web. Love to build things with quality, from the conception to the deploy. I take the software that I write very seriously while having an extreme passion for being the best I can be at doing what I love to do. Currently building great things at Stubhub, an eBay company, stay tuned. Specialties: Web development, web analytics, architect and produce solutions, high-scalability and performance.
Updated on May 04, 2020Comments
-
Charlie about 4 years
I am looking in the Collections framework of Java for a LIFO Structure (Stack) without any success. Basically I want a really simple stack; my perfect option would be a Deque, but I am in Java 1.5.
I would like not to have to add another class to my structure but I am wondering if that is possible:
Is there any class in the Collections framework (1.5) that does the job?
If not, is there any way to turn a Queue in a LIFO Queue (aka Stack) without reimplementation?
If not, which Interface or class should I extend for this task? I guess that keep the way that the guys of Sun have made with the Deque is a good start.
Thanks a lot.
EDIT: I forgot to say about the Stack class: I have my doubts about this class when I saw that it implements the Vector class, and the Vector class is a little bit obsolete, isn't it?
-
Ken Gentle over 15 yearsThe main issue with Vector is that all access is synchronized, whether you need it or not. It is as "up-to-date" as any of the other collections, but got a bad reputation due to the synchronization issue.
-
Spencer Kormos over 15 yearsLinkedList also then provides the definition of a Deque, which is your desired collection.
-
Charlie over 15 yearsSorry I forgot to Say about the Stack Class and my opinion about it, but I am thinking that probably is a better solution than implement my own class. isnt it?
-
dma_k about 11 years@SpencerKormos ... and
LinkedList
also supports null elements in contrast toArrayDeque
. -
Ivo over 6 yearsThe original post was using a Queue which was obviously FIFO and not LIFO, so I've updated my answer.
-
Basil Bourque about 4 yearsUPDATE This Answer is now outmoded. The
Stack
class has been supplanted by more modern classes, as explained in the Javadoc: 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. See the current solution in Answer by Brett Ryan and Answer by Ivan.