Skip to content
95 changes: 95 additions & 0 deletions content/java/concepts/queue/terms/size/size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
Title: 'size()'
Description: 'Returns the number of elements stored in a queue.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Collections'
- 'Methods'
- 'Numbers'
- 'Queues'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

In Java, **`size()`** method returns the number of elements currently present in a collection like a queue, helping determine how many items are stored at any given moment.

## Syntax

```pseudo
queue.size()
```

**Parameters:**

The `size()` method does not take any parameters.

**Return value:**

- Returns an integer representing the number of elements in the queue.

## Example 1: Get Queue Length using Java `size()`

In this example, the `size()` method in Java is used to find the number of elements present in a queue:

```java
import java.util.LinkedList;
import java.util.Queue;

public class QueueSizeExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();

queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");

System.out.println("Queue size: " + queue.size());
}
}
```

The output of this code is:

```shell
Queue size: 3
```

## Example 2: Check Queue Size Before Processing

In this example, the `size()` method in Java is used to verify if a queue contains elements before processing them:

```java
import java.util.LinkedList;
import java.util.Queue;

public class QueueSizeCheckExample {
public static void main(String[] args) {
Queue<Integer> numbers = new LinkedList<>();

numbers.add(10);
numbers.add(20);
numbers.add(30);

if (numbers.size() > 0) {
System.out.println("Queue has " + numbers.size() + " elements. Processing...");
while (!numbers.isEmpty()) {
System.out.println("Removed: " + numbers.remove());
}
} else {
System.out.println("Queue is empty. Nothing to process.");
}
}
}
```

The output of this code is:

```shell
Queue has 3 elements. Processing...
Removed: 10
Removed: 20
Removed: 30
```