Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
queue
size
communitycreator

What is queue.size() 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.size() function in Java is used to get the current size of a queue. queue.size() returns the number of elements in the queue.

The illustration below shows the visual representation of the queue.size() function.

Visual representation of queue.size() function

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

Syntax

public int size()

Parameter

The queue.size() function does not require any parameters.

Return value

This function returns the number of elements in the queue.

Code

import java.util.*;
class JAVA {
public static void main( String args[] ) {
Queue<Integer> Queue = new LinkedList<Integer>();
// adding elements in Queue
Queue.add(1);
Queue.add(3);
Queue.add(5);
Queue.add(2);
Queue.add(0);
System.out.println("Following are the elements in Queue: " + Queue);
System.out.println("Following is the size of Queue: " + Queue.size());
}
}

RELATED TAGS

java
queue
size
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