When you want a queue, use a PriorityQueue. When you want a Set, use a TreeSet. A TreeSet has unique elements, and doesn't offer the API of a Queue. A Queue doesn't offer the API of a Set, and allows multiple equal elements.
http://stackoverflow.com/questions/3524862/when-should-i-use-a-treemap-over-a-priorityqueue-and-vice-versa
Generally speaking, it is less work to track only the minimum element, using a heap.
A tree is more organized, and it requires more computation to maintain that organization. But if you need to access any key, and not just the minimum, a heap will not suffice, and the extra overhead of the tree is justified.
Read full article from data structures - Difference between PriorityQueue and TreeSet in Java? - Stack Overflow
No comments:
Post a Comment