What is queue.size() in Java?

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());
}
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources