Source of TestPriorityQueue1.java


  1: //TestPriorityQueue1.java
  2: //Here are the essential methods you want for a priority queue:
  3: //add()
  4: //remove()
  5: //peek()
  6: //size()
  7: //isEmpty()
  8: //clear()
  9: //This particular priority queue is the "default" version.

 11: import static java.lang.System.out;
 12: import java.util.Arrays;
 13: import java.util.PriorityQueue;

 15: public class TestPriorityQueue1
 16: {
 17:     public static void main(String[] args)
 18:     {
 19:         System.out.println("=====1=========================");
 20:         //Create a priority queue, confirm that it's empty,
 21:         //and display its size.
 22:         PriorityQueue<Integer> pqInt = new PriorityQueue<>();
 23:         if (pqInt.isEmpty())
 24:         {
 25:             out.println("The priority queue is empty.");
 26:         }
 27:         else
 28:         {
 29:             out.println
 30:             (
 31:                 "The size of the priority queue is "
 32:                 + pqInt.size() + "."
 33:             );
 34:         }

 36:         System.out.println("=====2=========================");
 37:         //Add some values to the priority queue, and then display the
 38:         //first (top priority) value and the size.
 39:         Integer[] a = { 2, 7, 5, 1, 4, 9 };
 40:         for (int i : a)
 41:         {
 42:             pqInt.add(i);
 43:         }
 44:         out.println
 45:         (
 46:             "The value at the front of the priority queue is "
 47:             + pqInt.peek() + "."
 48:         );
 49:         if (pqInt.isEmpty())
 50:         {
 51:             out.println("The priority queue is empty.");
 52:         }
 53:         else
 54:         {
 55:             out.println
 56:             (
 57:                 "The size of the priority queue is "
 58:                 + pqInt.size() + "."
 59:             );
 60:         }

 62:         System.out.println("=====3=========================");
 63:         //Display the priority queue as a single entity, along with its size.
 64:         out.println(pqInt);
 65:         if (pqInt.isEmpty())
 66:         {
 67:             out.println("The priority queue is empty.");
 68:         }
 69:         else
 70:         {
 71:             out.println
 72:             (
 73:                 "The size of the priority queue is "
 74:                 + pqInt.size() + "."
 75:             );
 76:         }

 78:         System.out.println("=====4=========================");
 79:         //Clear the priority queue by removing and displaying one value
 80:         //at a time. Then confirm that it's empty.
 81:         while (!pqInt.isEmpty()) //<--Cannot use for (int i : pqInt) here!
 82:         {
 83:             System.out.print(pqInt.remove() + " ");
 84:         }
 85:         out.println();
 86:         if (pqInt.isEmpty())
 87:         {
 88:             out.println("The priority queue is empty.");
 89:         }
 90:         else
 91:         {
 92:             out.println
 93:             (
 94:                 "The size of the priority queue is "
 95:                 + pqInt.size() + "."
 96:             );
 97:         }

 99:         System.out.println("=====5=========================");
100:         //Re-create the priority queue as before, but this time by
101:         //adding all the elements of an array after converting it
102:         //to a list. Then display the priority queue and its size.
103:         pqInt.addAll(Arrays.asList(a));
104:         out.println(pqInt);
105:         out.println(pqInt.size());

107:         System.out.println("=====6=========================");
108:         //Clear the priority queue once more and display it a final time.
109:         pqInt.clear();
110:         out.println(pqInt);
111:     }
112: }
113: /*  Output:
114:     =====1=========================
115:     The priority queue is empty.
116:     =====2=========================
117:     The value at the front of the priority queue is 1.
118:     The size of the priority queue is 6.
119:     =====3=========================
120:     [1, 2, 5, 7, 4, 9]
121:     The size of the priority queue is 6.
122:     =====4=========================
123:     1 2 4 5 7 9
124:     The priority queue is empty.
125:     =====5=========================
126:     [1, 2, 5, 7, 4, 9]
127:     6
128:     =====6=========================
129:     []
130: */