Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best implementation of Java Queue?

Tags:

java

queue

I am working (in Java) on a recursive image processing algorithm that recursively traverses the pixels of the image, outwards from a center point.

Unfortunately, that causes a Stack Overflow. 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 onto itself 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, 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)

like image 690
Georges Oates Larsen Avatar asked Jun 22 '12 03:06

Georges Oates Larsen


People also ask

What is the best way to implement queue in Java?

It's better to use ArrayDeque instead of LinkedList when implementing Stack and Queue in Java. ArrayDeque is likely to be faster than Stack interface (while Stack is thread-safe) when used as a stack, and faster than LinkedList when used as a queue.

What is the best way to implement queue?

Queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by using an Array. Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the array (starting the index of array from 0 ).

How do you implement a queue in Java?

While using linked list for the queue implementation, EnQueue operation is implemented by inserting element at the end of the list and DeQueue operation is implemented by deleting an element from the beginning of the list. You can visit my previous article for custom implementation of linked list in Java.

Which data type is best for implementing queue?

The deque class from the python collections module can also be used to implement a queue. This method of implementing a queue is far more efficient because deque provides faster enqueue and dequeue operations.


2 Answers

Use:

Queue<Object> queue = new LinkedList<>(); 

You can use .offer(E e) to append an element to the end of the queue and .poll() to dequeue and retrieve the head (first element) of the queue.

Java defined the interface Queue, the LinkedList provided an implementation.

It also maintains references to the Head and Tail elements, which you can get by .getFirst() and .getLast() respectively.


credit to @Snicolas for suggesting queue interface

like image 189
didxga Avatar answered Sep 20 '22 15:09

didxga


If you use LinkedList be careful. If you use it like this:

LinkedList<String> queue = new LinkedList<String>(); 

then you can violate queue definition, because it is possible to remove other elements than first (there are such methods in LinkedList).

But if you use it like this:

Queue<String> queue = new LinkedList<String>(); 

it should be ok,as this is heads-up to users that insertions should occur only at the back and deletions only at the front.

You can overcome defective implementation of the Queue interface by extending the LinkedList class to a PureQueue class that throws UnsupportedOperationException of any of the offending methods. Or you can take approach with aggreagation by creating PureQueue with only one field which is type LinkedList object, list, and the only methods will be a default constructor, a copy constructor, isEmpty(), size(), add(E element), remove(), and element(). All those methods should be one-liners, as for example:

/** * Retrieves and removes the head of this queue. * The worstTime(n) is constant and averageTime(n) is constant. * * @return the head of this queue. * @throws NoSuchElementException if this queue is empty. */ public E remove() {     return list.removeFirst(); } // method remove() 
like image 31
azec-pdx Avatar answered Sep 20 '22 15:09

azec-pdx