tags:

views:

642

answers:

3

I am looking for a fast queue implementation in Java. I see that LinkedList implements the Queue interface, but it will only be as fast as a LinkedList right? Is there a way to have a queue that will be faster especially for add (I only need poll, add and check for empty). Down the line I may also need a PriorityQueue but not yet.

+6  A: 

If multiple threads are going to be accessing the queue then consider using an ArrayBlockingQueue. Otherwise take a look at ArrayDeque. From the ArrayDeque API:

This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

Specifically an array-based queue implementation reduces the need to resize the underlying array if the existing array has sufficient capacity, thus making additions to the queue generally faster than LinkedList. Be aware that ArrayBlockingQueue is a bounded implementation whereas ArrayDeque will resize as required.

The flip-side is that LinkedList will typically provide a much more compact representation, particularly in cases where your queue grows and shrinks by a large amount. For example, if you added 10,000,000 elements to an ArrayDeque and then removed 9,999,999 elements, the underlying array would still be of length 10,000,000 whereas a LinkedList would not suffer from this problem.

In reality, for single-threaded access to a non-blocking queue I tend to favour LinkedList. I imagine the performance differences are so negligable you wouldn't notice the difference anyway.

Adamski
LinkedBlockingQueue is actually faster then the ArrayBlockingQueue. So if you were using a concurrent queue that is blocking use that one. Else using ConcurrentLinkedQueue is faster then both those two.
John V.
+10  A: 

I see that LinkedList implements the Queue interface, but it will only be as fast as a LinkedList right?

Eyeballing the source code, LinkedList is O(1) for Queue.add, Queue.poll, and Queue.peek operations.

I hope that's fast enough.

Noel Ang
I was thinking LinkedList add would be `O(n)` for some reason.
Eqbal
That would be an exceedingly stupid accomplishment for a linked-list data structure.
Noel Ang
Linked lists are generally O(n) for searching, but if you're actually using it as a queue it won't ever come up.
Graphics Noob
@Eqbal: That would be true if it was implemented as a singly-linked list. But it's a doubly-linked list, so you can jump to the end quickly.
Jay
Nothing prevents even a singly-linked list implementation from being able to jump to the end quickly. That's an implementation detail.
Noel Ang
+2  A: 

If performance of a linked list was really a problem, an alternative would be to implement a "circular queue" in an array, i.e. a queue where the start and end point move as entries are added and deleted. I can give more details if you care. When I was using languages that did not have a library of collections, this was how I always implemented queues because it was easier to write than a linked list and it was faster. But with built-in collections, the effort of writing and debugging my own collection for a special case is not worth the trouble 99% of the time: When it's already written, the fact that I could write it a different way faster than I could re-write it the way Java does is pretty much an irrelevant fact. And any performance gain is likely to be too small to be worth the trouble. I sub-type existing collections to get special behavior I need now and then, but I'm hard-pressed to think of the last time that I wrote one from scratch.

Jay
Upvoting because this is a good alternative to the selected answer if the queue speed is profiled to be a performance bottleneck.
Merlyn Morgan-Graham