Georges Oates Larsen Georges Oates Larsen - 1 year ago 69
Java Question

Best implementation of Java Queue?

I am working (In java) on a recursive image processing algorithm that recursively traverses the pixels of the image, outward from a center point.

Unfortunately... That causes stack overflows, so I have decided to switch to a Queue-based algorithm.

Now, this is all fine and dandy -- But considering the fact that its queue will be analyzing THOUSANDS of pixels in a very short amount of time, while constantly popping and pushing, WITHOUT maintaining a predictable state (It could be anywhere between length 100, and 20000); The queue implementation needs to have significantly fast popping and pushing abilities.

A linked list seems attractive due to its ability to push elements unto its self without rearranging anything else in the list, but in order for it to be fast enough, it would need easy access to both its head, AND its tail (or second-to-last node if it were not doubly-linked). Sadly, though I cannot find any information related to the underlying implementation of linked lists in Java, so it's hard to say if a linked list is really the way to go...

This brings me to my question... What would be the best implementation of the Queue interface in Java for what I intend to do? (I do not wish to edit or even access anything other than the head and tail of the queue -- I do not wish to do any sort of rearranging, or anything. On the flip side, I DO intend to do a lot of pushing and popping, and the queue will be changing size quite a bit, so preallocating would be inefficient)

Answer Source

LinkedList seems to a way to go, LinkedList is a doubly linked list, which is good for a Queue data structure (FIFO). It maintains a Head/Tail references, which you can get by getFirst() and getLast().