What is queue.add() in Java?

The queue.add() function in Java is used to insert an element at the back of the queue.

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

Figure 1: Visual representation of queue.add() function

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

Syntax

boolean queue_name.add(element)

queue_name is the name of the queue.

Parameter

This function requires an element as a parameter.

Return value

This function inserts the element sent as a parameter at the back of the queue and returns true.

If there is no capacity in a queue, then it returns an IllegalStateException.

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 before adding 0= 1->3->5->2
System.out.println("Following are the elements in Queue before 0: " + Queue);
System.out.println("0 entered at the back of Queue: "+ Queue.add(0));
//Queue after adding 0 = 1->3->5->2->0
System.out.println("Following are the elements in Queue after 0: " + Queue);
}
}