• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best implementation for Queue interface.

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be best implementation for this interface?
I'll probably need thread safe. Is there already one? Any tips before I start?
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to choose the best way to do something, use PriorityQueue. The infamous traveling salesman problem benefits from a priority queue. In that problem the have to find the best route to a destination, and there are many possibilities. A LinkedList is good if your doing many insertions or deletions.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There probably isn't a "best" implementation of anything, but have a look at the java.util.Queue interface, where there is a list of implementing classes. Some of them may have instructions about how to make a Queue thread-safe. There are comments about blocking queues in Queue, too.
 
Clyde DeSouza
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html


read the docs for the Use of the Queue!
 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this. So let me rephrase. Let's say I do not need thread safe, do not care about nulls. I just want fast and the least resources if possible.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayDeque
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or LinkedList

 
John Landon
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's what I thought. Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation says java.util.ArrayDeque is faster than LinkedList.
 
Clyde DeSouza
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As garett said LinkedList would be more effective!!!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The documentation says java.util.ArrayDeque is faster than LinkedList.


I hadn't seen that. Some quick tests confirm that ArrayDeque is indeed faster for add() and remove() operations than LinkedList.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they have managed to keep ArrayDeque well hidden
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic