Consider the following program:
import java.util.PriorityQueue; public class Main { public static void main(String []args) { PriorityQueue<Integer> someValues = new PriorityQueue<Integer>(); someValues.add(new Integer(10)); someValues.add(new Integer(15)); someValues.add(new Integer(5)); Integer value; while ((value = someValues.poll()) != null) { System.out.print(value + " "); }/* w ww . j av a2 s. c om*/ } }
When executed, this program prints the following:
c)
The PriorityQueue prioritizes the elements in the queue according to its "natural ordering."
For integers, natural ordering is in ascending order, thus the output.
Note that the poll()
method retrieves and removes the head of the queue if an element is available, or it returns null if the queue is empty.