Given:
1. import java.util.*; 2. public class Main { 3. public static void main(String[] args) { 4. PriorityQueue toDo = new PriorityQueue(); 5. toDo.add("dishes"); 6. toDo.add("laundry"); 7. toDo.add("bills"); 8. toDo.offer("bills"); 9. System.out.print(toDo.size() + " " + toDo.poll()); 10. System.out.print(" " + toDo.peek() + " " + toDo.poll()); 11. System.out.println(" " + toDo.poll() + " " + toDo.poll()); 12. }//from w ww. j a va2s .co m 13. }
What is the result?
E is correct.
The add()
and offer()
methods have the same functionality, and it's okay for PriorityQueue to have duplicate elements.
Remembering that PriorityQueue are sorted, the poll()
method removes the first element in the queue and returns it.
The peek()
method returns the first element from the queue but does NOT remove it.