@@ -64,6 +64,10 @@ public static void main(String[] args) {
6464 * E remove() : Declared in the Queue interface. Removes the element at the head
6565 * of the queue returning the element in the process. It throws
6666 * NoSuchElementException if the queue is empty.
67+ *
68+ * boolean remove(Object obj) : Declared in the Collection interface. Removes
69+ * one instance of obj from the queue. Returns true if the element was removed.
70+ * Otherwise, returns false.
6771 */
6872
6973 demoQueue .remove ();
@@ -76,6 +80,14 @@ public static void main(String[] args) {
7680
7781 System .out .println ("Element removed = " + removedElement ); // Element removed = 10
7882
83+ demoQueue .add (10 );
84+ // demoQueue = [50, 20, 40, 10]
85+
86+ demoQueue .remove (10 );
87+ // demoQueue = [50, 20, 40]
88+
89+ System .out .println ("demoQueue = " + demoQueue ); // demoQueue = [50, 20, 40]
90+
7991 /*
8092 * Get the element at the head of the queue
8193 *
@@ -155,43 +167,43 @@ public static void main(String[] args) {
155167 // demoQueue = []
156168
157169 System .out .println ("demoQueue = " + demoQueue ); // demoQueue = []
158-
170+
159171 /*
160172 * Construct queue from array
161173 */
162-
163- String fruits [] = {"apple" , "grape" , "banana" , "orange" };
164-
174+
175+ String fruits [] = { "apple" , "grape" , "banana" , "orange" };
176+
165177 Queue <String > fruitQueue = new LinkedList <>();
166-
178+
167179 Collections .addAll (fruitQueue , fruits );
168-
180+
169181 System .out .println ("fruitQueue = " + fruitQueue ); // fruitQueue = [apple, grape, banana, orange]
170-
182+
171183 /*
172184 * Construct array from queue
173185 */
174-
186+
175187 String fruitArr [] = fruitQueue .toArray (new String [fruitQueue .size ()]);
176-
188+
177189 System .out .println ("fruitArr = " + Arrays .toString (fruitArr )); // fruitArr = [apple, grape, banana, orange]
178-
190+
179191 /*
180192 * Iterating over the contents of a queue
181193 */
182-
194+
183195 Iterator itr = fruitQueue .iterator ();
184-
196+
185197 while (itr .hasNext ()) {
186198 System .out .print (itr .next () + " " );
187199 }
188-
200+
189201 System .out .println ();
190-
202+
191203 for (String fruit : fruitQueue ) {
192204 System .out .print (fruit + " " );
193205 }
194-
206+
195207 }
196208
197209}
0 commit comments