PriorityQueue and TreeSet are classes from the Java Collections Framework, but they serve different purposes. PriorityQueue is designed for processing elements based on priority, while TreeSet stores unique elements in a sorted order. This article explains their differences with clear examples and a comparison table.
PriorityQueue
A PriorityQueue is used when elements need to be processed according to priority rather than insertion order. It implements the Queue interface and internally uses a heap-based data structure.
- Allows duplicate elements.
- Orders elements using natural ordering or a provided Comparator.
- Only the head element is guaranteed to be the smallest (or highest priority).
When to use PriorityQueue
- When elements must be processed based on priority.
- When frequent access to the smallest or largest element is required.
- When duplicate elements are allowed.Hey Geek !!
Thanks for pointing out, we have updated the article and the changes will be reflected soon. - When full sorting of all elements is not required.
Example:
import java.util.*;
class PriorityQueueDemo {
public static void main(String args[])
{
PriorityQueue<String> pQueue = new PriorityQueue<>();
pQueue.add("Geeks");
pQueue.add("For");
pQueue.add("Geeks");
System.out.println(pQueue.peek());
System.out.println(pQueue.poll());
System.out.println(pQueue.peek());
}
}
Output
For For Geeks
Explanation:
- Elements are arranged based on natural ordering.
- peek() returns the highest-priority element without removing it.
- poll() removes and returns the highest-priority element.
TreeSet
TreeSet is an implementation of the SortedSet interface that stores unique elements in sorted order. It is backed by a Red-Black Tree and also implements NavigableSet.
- Does not allow duplicate elements.
- Maintains sorted order at all times.
- Ordering must be consistent with equals().
When to use TreeSet
- When elements must remain sorted at all times.
- When duplicate elements are not allowed.
- When range-based or navigational operations are needed.
- When ordered iteration is required.
Example:
import java.util.*;
class TreeSetDemo {
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String>();
ts.add("Geek");
ts.add("For");
ts.add("Geeks");
System.out.println("Tree Set is " + ts);
String check = "Geeks";
System.out.println("Contains " + check + " "
+ ts.contains(check));
System.out.println("First Value " + ts.first());
System.out.println("Last Value " + ts.last());
String val = "Geek";
System.out.println("Higher " + ts.higher(val));
System.out.println("Lower " + ts.lower(val));
}
}
Output
Tree Set is [For, Geek, Geeks] Contains Geeks true First Value For Last Value Geeks Higher Geeks Lower For
Explanation:
- Elements are stored in sorted order automatically.
- first() and last() return boundary elements.
- higher() and lower() perform navigational operations.
PriorityQueue Vs TreeSet
Understanding how these two collections differ helps in choosing the right data structure for priority-based or sorted data processing.
Feature | PriorityQueue | TreeSet |
|---|---|---|
Interface Implemented | Queue | SortedSet, NavigableSet |
Data Structure | Heap | Red-Black Tree |
Duplicate Elements | Allowed | Not Allowed |
Ordering Guarantee | Only head is ordered | Entire set is sorted |
Access to Min/Max | O(1) for head element | O(1) for first/last |
Introduced In | JDK 1.5 | JDK 1.4 |