views:

2678

answers:

4

Hi, 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:

  1. Is there any class in the Collections framework (1.5) that does the job?

  2. If not, is there any way to turn a Queue in a LIFO Queue (aka Stack) without reimplementation?

  3. 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?

+3  A: 

There is a Stack class in the API. Will this meet your needs?

Greg
Sorry 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?
David Santamaria
Don't use the Stack class. It extends Vector, which is retained for backwards compatibility only.
erickson
+14  A: 

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.

Eli Courtwright
LinkedList also then provides the definition of a Deque, which is your desired collection.
Spencer K
Yes, I think that LinkedList is the one that i was looking for, cause in my first look at it i didnt realise about the Addfirst and removeFirst methods. Thanks a lot.
David Santamaria
A: 

Look in the google collections or the apache collections.
The google version supports generics.

damian
A: 

Stack class is slowly: methods are synchronized + Stack extends synchronized Vector