views:

513

answers:

8

Hello,

Which implementation is less "heavy": PriorityQueue or a sorted LinkedList (using a Comparator)?

I want to have all the items sorted. The insertion will be very frequent and ocasionally I will have to run all the list to make some operations.

Thank you!

+2  A: 

You should implement both and then do performance testing on actual data to see which works best in your specific circumstances.

nohat
Is there ever a circumstance where LinkedList would actually be faster for maintaining sorted entries with frequent inserts?
Jeff Storey
Yes. If the inserts happen very often, and the lookups don't, it might be best to just appened and sort on the lookup. You can cache whether it's been sorted or not. It depends on how often each occurs. That's where the testing comes in. This really should be a test question in a sophomore Data Structures class.
glowcoder
I meant if the list needed to be constantly sorted and not only sorted on demand. No need to be condescending.
Jeff Storey
I'd go with @Jeff's TreeSet. No need to implement and test.In a Linked List, both the insert and lookup will be O(n) assuming the list is always sorted. That's because unlike Arrays or ArrayLists, LinkedList will have to iterate from beginning to nth index to get the nth element, so you can't efficiently do Binary Search. The inserts are O(n) as well because it will first have to search the location to insert (assuming list is sorted) even though the insert operation itself will be O(1).
Cem Catikkas
+1  A: 

If you use a LinkedList, you would need to resort the items each time you added one and since inserts are frequent, I wouldn't use a LinkedList. So in this case, I would use a PriorityQueue's If you will only be adding unique elements to the list, I recommend using a SortedSet (one implementation is the TreeSet).

Jeff Storey
An insert on PriorityQuery is documented as O(ln n). (Insertion into a LinkedList is O(n))
Kathy Van Stone
`java.util.LinkedList` is optimized for adding elements to the end of the list, making it an O(1) operation. However, sorting a `LinkedList` has horrible performance.
erickson
@erickson: Even for first and last inserts there is a better alternative in Java 6: `java.util.ArrayDeque`.
Alexander Pogrebnyak
A: 

Do you need it sorted at all times? If that's the case, you might want to go with something like a tree-set (or other SortedSet with a fast lookup).

If you only need it sorted occasionally, go with a linked list and sort it when you need access. Let it be unsorted when you don't need access.

glowcoder
A: 

java.util.PriorityQueue is

"An unbounded priority queue based on a priority heap"

. The heap data structure make much more sense than a linked list

ring bearer
+6  A: 

A LinkedList is the worst choice. Either use an ArrayList (or, more generally, a RandomAccess implementor), or PriorityQueue. If you do use a list, sort it only before iterating over its contents, not after every insert.

One thing to note is that the PriorityQueue iterator does not provide the elements in order; you'll actually have to remove the elements (empty the queue) to iterate over its elements in order.

erickson
+1  A: 

There is a fundamental difference between the two data structures and they are not as easily interchangeable as you might think.

According to the PriorityQueue documentation:

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order.

Use an ArrayList and call Collections.sort() on it only before iterating the list.

Tim Bender
A: 

The issue with PriorityQueue is that you have to empty the queue to get the elements in order. If that is what you want then it is a fine choice. Otherwise you could use an ArrayList that you sort only when you need the sorted result or, if the items are distinct (relative to the comparator), a TreeSet. Both TreeSet and ArrayList are not very 'heavy' in terms of space; which is faster depends on the use case.

Kathy Van Stone
A: 

I can see two options, which one is better depends on whether you need to be able to have duplicate items.

If you don't need to maintain duplicate items in your list, I would use a SortedSet (probably a TreeSet).

If you need maintain duplicate items, I would go with an LinkedList and insert new items into the list in the correct order.

The PriorityQueue doesn't really fit unless you want to remove the items whenever you do operations.

Going along with the others, make sure you use profiling to make sure you're picking out the correct solution for your particular problem.

deterb