29

I am trying to understand when to use the two data structures. As far as I have understood the PriorityQueue is also implemented as a tree as the documentation states that the average time for insert, remove and contains is O(log(n)). The TreeSet also provides the same time complexity. Plus both of them are unsynchorized implementation. And I can write comparator for them to act like min heap or max heap.

Can some one point out in what conditions I use these two sets?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Fox
  • 9,384
  • 13
  • 42
  • 63

1 Answers1

43

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255