☕ Java
Intermediate
What is PriorityQueue in Java?
Answer
A PriorityQueue is a queue where elements are ordered by their natural ordering (if they implement Comparable) or by a provided Comparator, not by insertion order. The head of the queue is always the smallest element (min-heap by default). Operations: offer() adds an element (O(log n)), poll() removes and returns the head (O(log n)), peek() returns the head without removing (O(1)). PriorityQueue does not permit null elements. Iteration over a PriorityQueue does not guarantee any particular order — only the head is guaranteed to be the minimum. Use it for scheduling algorithms, Dijkstra's shortest path, and any problem requiring "process the most important item first" semantics.
More Java Questions
View all →- Intermediate What is the Java Collections Framework?
- Intermediate What is the difference between ArrayList and LinkedList?
- Intermediate What is HashMap in Java and how does it work internally?
- Intermediate What is the difference between HashMap and HashTable in Java?
- Intermediate What is the difference between HashMap and LinkedHashMap?