From 14914f1f7c5d4f89b7042f5bd26500b5594c2940 Mon Sep 17 00:00:00 2001 From: Ilham Gilang Utama Date: Fri, 8 Aug 2025 10:58:02 +0700 Subject: [PATCH 1/2] add size.md for Issue Java Queue: size() --- .../java/concepts/queue/terms/size/size.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 content/java/concepts/queue/terms/size/size.md diff --git a/content/java/concepts/queue/terms/size/size.md b/content/java/concepts/queue/terms/size/size.md new file mode 100644 index 00000000000..271dec93749 --- /dev/null +++ b/content/java/concepts/queue/terms/size/size.md @@ -0,0 +1,78 @@ +--- +Title: 'size' +Description: 'Returns the number of elements in a queue in Java.' +Subjects: + - 'java' + - 'queues' +Tags: + - 'java' + - 'queue-methods' + - 'collections' +CatalogContent: + - 'learn-java' + - 'paths/java-fundamentals' +--- + +**size()** is a method in Java that returns the number of elements currently present in a collection like a queue. It helps to determine how many items are stored in the queue at any given moment. + +## Syntax + +```java +int size() +``` + +- Takes no parameters. +- Returns an integer representing the count of elements in the queue. +- Available in classes implementing the `Queue` interface, such as `LinkedList` and `PriorityQueue`. + +## Example + +```java +import java.util.LinkedList; +import java.util.Queue; + +public class QueueSizeExample { + public static void main(String[] args) { + Queue queue = new LinkedList<>(); + + queue.add("Apple"); + queue.add("Banana"); + queue.add("Cherry"); + + System.out.println("Queue size: " + queue.size()); // Output: Queue size: 3 + } +} +``` + +## Codebyte Example (if applicable) + +We can currently support: + +- Python +- JavaScript +- Ruby +- C++ +- C# +- Go +- PHP + +See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! + +```codebyte/js +// Example runnable Java code to illustrate size concept using Queue interface +import java.util.LinkedList; +import java.util.Queue; + +public class QueueSizeExample { + public static void main(String[] args) { + Queue queue = new LinkedList<>(); + + queue.add("Apple"); + queue.add("Banana"); + queue.add("Cherry"); + + System.out.println("Queue size: " + queue.size()); // Output: Queue size: 3 + } +} +``` + From 32070f1f435cccc9c965ebc868d1e918c1a6d9de Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 13 Aug 2025 13:17:32 +0530 Subject: [PATCH 2/2] content tweaks and changed example 2 --- .../java/concepts/queue/terms/size/size.md | 98 +++++++++++-------- 1 file changed, 57 insertions(+), 41 deletions(-) diff --git a/content/java/concepts/queue/terms/size/size.md b/content/java/concepts/queue/terms/size/size.md index 271dec93749..f4075564d36 100644 --- a/content/java/concepts/queue/terms/size/size.md +++ b/content/java/concepts/queue/terms/size/size.md @@ -1,78 +1,94 @@ --- -Title: 'size' +Title: 'size()' Description: 'Returns the number of elements in a queue in Java.' Subjects: - - 'java' - - 'queues' + - 'Code Foundations' + - 'Computer Science' Tags: - - 'java' - - 'queue-methods' - - 'collections' + - 'Collections' + - 'Queues' CatalogContent: - 'learn-java' - - 'paths/java-fundamentals' + - 'paths/computer-science' --- -**size()** is a method in Java that returns the number of elements currently present in a collection like a queue. It helps to determine how many items are stored in the queue at any given moment. +Java **`size()`** is a method that 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 -```java -int size() +```pseudo +queue.size() ``` -- Takes no parameters. +**Parameters:** + +`size()` method does not take any parameters. + +**Return value:** + - Returns an integer representing the count of elements in the queue. -- Available in classes implementing the `Queue` interface, such as `LinkedList` and `PriorityQueue`. -## Example +## 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 queue = new LinkedList<>(); + public static void main(String[] args) { + Queue queue = new LinkedList<>(); - queue.add("Apple"); - queue.add("Banana"); - queue.add("Cherry"); + queue.add("Apple"); + queue.add("Banana"); + queue.add("Cherry"); - System.out.println("Queue size: " + queue.size()); // Output: Queue size: 3 - } + System.out.println("Queue size: " + queue.size()); // Output: Queue size: 3 + } } ``` -## Codebyte Example (if applicable) +The output of this code is: -We can currently support: +```shell +Queue size: 3 +``` -- Python -- JavaScript -- Ruby -- C++ -- C# -- Go -- PHP +## Example 2: Check Queue Before Processing -See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! +In this example, the `size()` method in Java is used to verify if a queue contains elements before processing them: -```codebyte/js -// Example runnable Java code to illustrate size concept using Queue interface +```java import java.util.LinkedList; import java.util.Queue; -public class QueueSizeExample { - public static void main(String[] args) { - Queue queue = new LinkedList<>(); - - queue.add("Apple"); - queue.add("Banana"); - queue.add("Cherry"); - - System.out.println("Queue size: " + queue.size()); // Output: Queue size: 3 +public class QueueSizeCheckExample { + public static void main(String[] args) { + Queue 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 +``` +