What is queue.remove() in Java?

The queue.remove() function in Java returns the element available at the front of the queue and removes that element from the queue.

Figure 1, below, shows the visual representation of the queue.remove() function.

Figure 1: Visual representation of the queue.remove() function

Required modules

The following module is required to use the java.util.* function.

Syntax


element queue_name.remove();
// where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

This function returns the element available at the front of the queue and removes that element from the queue.

Code

import java.util.*;
class JAVA {
public static void main( String args[] ) {
Queue<Integer> Queue = new LinkedList<Integer>();
Queue.add(1);
Queue.add(3);
Queue.add(5);
Queue.add(2);
Queue.add(0);
//Queue = 1->3->5->2->0
System.out.println("Following are the elements in Queue before removing: " + Queue);
System.out.println("The element removed from the front of Queue: "+Queue.remove());
System.out.println("Following are the elements in Queue after removing: " + Queue);
}
}

Free Resources