Source of TestPriorityQueue2.java


  1: //TestPriorityQueue2.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 a version that uses
 10: //a lambda function to reverse the priority.

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

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

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

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

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

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

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