Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
queue
add
communitycreator

What is queue.add() in Java?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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);
}
}

RELATED TAGS

java
queue
add
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring