Source of TestArrayDequeAsQueue.java


  1: //TestArrayDequeAsQueue.java
  2: //Here are the essential methods you want for a queue,
  3: //to provide the required FIFO behavior:
  4: //add()
  5: //remove()
  6: //peek()
  7: //size()
  8: //isEmpty()
  9: //clear()
 10: //So, the question is: Does an ArrayDeque provide them?
 11: //And the answer, of course, is yes ... along with many
 12: //other methods that are not essential for a queue, but
 13: //are inherited from AbstractCollection<E>, Collection<E>,
 14: //and Iterable<E>, and that should not be used if one is
 15: //adhering strictly to the notion of a FIFO queue.

 17: import static java.lang.System.out;
 18: import java.util.ArrayDeque;
 19: import java.util.Queue;
 20: import java.util.Arrays;

 22: public class TestArrayDequeAsQueue
 23: {
 24:     public static void main(String[] args)
 25:     {
 26:         System.out.println("=====1=========================");
 27:         //Create a queue, confirm that it's empty, and display its size.
 28:         Queue<Integer> qInt = new ArrayDeque<>();
 29:         if (qInt.isEmpty())
 30:         {
 31:             out.println("The queue is empty.");
 32:         }
 33:         else
 34:         {
 35:             out.println("The size of the queue is " + qInt.size() + ".");
 36:         }

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

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

 72:         System.out.println("=====4=========================");
 73:         //Clear the queue by removing and displaying one value
 74:         //at a time. Then confirm that it's empty.
 75:         //for (int i : qInt) //<--Can use this for-loop here.
 76:         //Unlike in the priority queue.
 77:         while (!qInt.isEmpty())
 78:         {
 79:             System.out.print(qInt.remove() + " ");
 80:         }
 81:         out.println();
 82:         if (qInt.isEmpty())
 83:         {
 84:             out.println("The queue is empty.");
 85:         }
 86:         else
 87:         {
 88:             out.println("The size of the queue is " + qInt.size() + ".");
 89:         }

 91:         System.out.println("=====5=========================");
 92:         //Re-create the queue as before, but this time by adding
 93:         //all the elements of an array after converting it to a
 94:         //list. Then display the queue and its size.
 95:         qInt.addAll(Arrays.asList(a));
 96:         out.println(qInt);
 97:         out.println(qInt.size());

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