Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java 1.5 - Queue Data Type

Started by eben Sep 24, 2005 at 5:28 PM 2 replies 1k views
Original Post
eben
eben
I was looking through the Java 1.5 api for a basic Queue implementation (something like ArrayQueue from Java 1.4) But looking at all the classes that extend the Queue interface I'm confused because none of them seem to be a basic ol' Queue. linky: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html Any suggestions? thanks.
bleb
bleb
What's wrong with:

Queue myQueue = new LinkedList();

That way, whenever you use myQueue, you only have access to the methods defined in the queue interface.
Also, a linked list is the ideal data structure to use to back a queue interface from an execution speed viewpoint.
Son of Cain
Son of Cain
Bleb is right - LinkedList implements the Queue interface, and it's one of the most basic and fast implementations of it.
a.k.a javabeats at yahoo.ca
Lucidquiet
Lucidquiet
The only thing possibly faster than that would be a Circular Array Queue, which wouldn't be hard to create yourself, but the basic problem would be that you would have to know how large to make it when instantiating it. Of course, you could always "grow" it when it gets too large, but if your going to have to "grow" the thing then a LinkedList is better. The only other possible side problem with a LinkedList implementation is the added storage required by each node to hold the Link Reference. But that isn't really all to big a deal most of the time. (Heck you probably already know all this).

Another solution would be to write a class that extends ArrayList, and Implements the Queue interface. And you could do that as an annonymous class, that just forwards the Queue interface calls to the appropriate ArrayList methods calls.

L-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.