From d8131dca3ce93531b32a586a24dfa182aee210e9 Mon Sep 17 00:00:00 2001 From: Mohit5Upadhyay Date: Fri, 25 Jul 2025 13:51:12 +0530 Subject: [PATCH 1/3] add: java queue .peek() method term entry --- .../java/concepts/queue/terms/peek/peek.md | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 content/java/concepts/queue/terms/peek/peek.md diff --git a/content/java/concepts/queue/terms/peek/peek.md b/content/java/concepts/queue/terms/peek/peek.md new file mode 100644 index 00000000000..673355afe6c --- /dev/null +++ b/content/java/concepts/queue/terms/peek/peek.md @@ -0,0 +1,155 @@ +--- +Title: '.peek()' +Description: 'Returns the head element of the queue without removing it, or null if the queue is empty.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Collections' + - 'Queues' + - 'Data Structures' + - 'Methods' + - 'Algorithms' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`.peek()`** method retrieves the head element of a [Queue](https://www.codecademy.com/resources/docs/java/queue) without removing it from the queue. If the queue is empty, it returns `null` instead of throwing an exception. This makes it a safe way to check what element is next in line without modifying the queue structure. + +## Syntax + +```pseudo +queueName.peek() +``` + +**Return value:** Returns the head element of the queue, or `null` if the queue is empty. + +## Example 1: Usage of `.peek()` Method + +This example demonstrates how `.peek()` allows you to inspect the next element without removing it from the queue: + +```java +import java.util.Queue; +import java.util.LinkedList; + +public class Main { + public static void main(String[] args) { + // Create a queue for customer service + Queue customerQueue = new LinkedList<>(); + + // Add customers to the queue + customerQueue.offer("Alice"); + customerQueue.offer("Bob"); + customerQueue.offer("Charlie"); + + System.out.println("Queue: " + customerQueue); + + // Peek at the next customer without removing them + String nextCustomer = customerQueue.peek(); + System.out.println("Next customer to be served: " + nextCustomer); + + // Queue remains unchanged after peek + System.out.println("Queue after peek: " + customerQueue); + System.out.println("Queue size: " + customerQueue.size()); + + // Now actually serve the customer + String servedCustomer = customerQueue.poll(); + System.out.println("Served customer: " + servedCustomer); + System.out.println("Updated queue: " + customerQueue); + } +} +``` + +The output of this code is: + +```shell +Queue: [Alice, Bob, Charlie] +Next customer to be served: Alice +Queue after peek: [Alice, Bob, Charlie] +Queue size: 3 +Served customer: Alice +Updated queue: [Bob, Charlie] +``` + +This example shows how `.peek()` provides read-only access to the head element, keeping the queue unchanged until an actual removal operation is performed. + + +## Example 2: Handling Empty Queues with `.peek()` + +This example shows how `.peek()` safely handles empty queues by returning `null` instead of throwing exceptions: + +```java +import java.util.Queue; +import java.util.LinkedList; + +public class EmptyQueueHandling { + public static void main(String[] args) { + Queue messageQueue = new LinkedList<>(); + + // Peek at empty queue + String result = messageQueue.peek(); + System.out.println("Peek on empty queue: " + result); + System.out.println("Queue is empty: " + messageQueue.isEmpty()); + + // Add some messages + messageQueue.offer("Welcome message"); + messageQueue.offer("Alert notification"); + + // Peek at non-empty queue + String nextMessage = messageQueue.peek(); + System.out.println("Next message: " + nextMessage); + + // Process all messages while checking what's next + while (!messageQueue.isEmpty()) { + String current = messageQueue.peek(); + System.out.println("About to process: " + current); + + String processed = messageQueue.poll(); + System.out.println("Processed: " + processed); + + // Check if there are more messages + String upcoming = messageQueue.peek(); + if (upcoming != null) { + System.out.println("Next up: " + upcoming); + } else { + System.out.println("No more messages in queue"); + } + System.out.println("---"); + } + } +} +``` + +The output of this code is: + +```shell +Peek on empty queue: null +Queue is empty: true +Next message: Welcome message +About to process: Welcome message +Processed: Welcome message +Next up: Alert notification +--- +About to process: Alert notification +Processed: Alert notification +No more messages in queue +--- +``` + +This example demonstrates the safety of `.peek()` when dealing with potentially empty queues, making it ideal for defensive programming practices. + + +## Frequently Asked Questions + +### 1. What is the difference between `.peek()` and `.element()` methods? + +Both methods return the head element without removing it, but `.element()` throws a `NoSuchElementException` if the queue is empty, while `.peek()` returns `null`. Use `.peek()` for safer code that handles empty queues gracefully. + +### 2. When should I use `.peek()` instead of `.poll()`? + +Use `.peek()` when you want to inspect the next element without removing it from the queue. This is useful for conditional processing, monitoring systems, or when you need to check what's next before deciding whether to process it. + +### 3. Can `.peek()` be used with all Queue implementations? + +Yes, `.peek()` is defined in the Queue interface and is available in all implementations including LinkedList, ArrayDeque, and PriorityQueue. However, the ordering of elements may differ based on the implementation (e.g., PriorityQueue orders by priority, not insertion order). From dc9d156be402d56753e7e8891e1f18e3398fa126 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 1 Aug 2025 16:11:32 +0530 Subject: [PATCH 2/3] minor tweaks --- .../java/concepts/queue/terms/peek/peek.md | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/content/java/concepts/queue/terms/peek/peek.md b/content/java/concepts/queue/terms/peek/peek.md index 673355afe6c..ea75f8445ea 100644 --- a/content/java/concepts/queue/terms/peek/peek.md +++ b/content/java/concepts/queue/terms/peek/peek.md @@ -5,11 +5,11 @@ Subjects: - 'Code Foundations' - 'Computer Science' Tags: + - 'Algorithms' - 'Collections' - - 'Queues' - 'Data Structures' - 'Methods' - - 'Algorithms' + - 'Queues' CatalogContent: - 'learn-java' - 'paths/computer-science' @@ -23,11 +23,13 @@ The **`.peek()`** method retrieves the head element of a [Queue](https://www.cod queueName.peek() ``` -**Return value:** Returns the head element of the queue, or `null` if the queue is empty. +**Return value:** + +Retrieves the head of the queue without removing it. Returns `null` if the queue is empty. ## Example 1: Usage of `.peek()` Method -This example demonstrates how `.peek()` allows you to inspect the next element without removing it from the queue: +This example demonstrates how `.peek()` is used to inspect the next element without removing it from the queue: ```java import java.util.Queue; @@ -53,7 +55,7 @@ public class Main { System.out.println("Queue after peek: " + customerQueue); System.out.println("Queue size: " + customerQueue.size()); - // Now actually serve the customer + // Serve the customer String servedCustomer = customerQueue.poll(); System.out.println("Served customer: " + servedCustomer); System.out.println("Updated queue: " + customerQueue); @@ -74,7 +76,6 @@ Updated queue: [Bob, Charlie] This example shows how `.peek()` provides read-only access to the head element, keeping the queue unchanged until an actual removal operation is performed. - ## Example 2: Handling Empty Queues with `.peek()` This example shows how `.peek()` safely handles empty queues by returning `null` instead of throwing exceptions: @@ -138,18 +139,3 @@ No more messages in queue ``` This example demonstrates the safety of `.peek()` when dealing with potentially empty queues, making it ideal for defensive programming practices. - - -## Frequently Asked Questions - -### 1. What is the difference between `.peek()` and `.element()` methods? - -Both methods return the head element without removing it, but `.element()` throws a `NoSuchElementException` if the queue is empty, while `.peek()` returns `null`. Use `.peek()` for safer code that handles empty queues gracefully. - -### 2. When should I use `.peek()` instead of `.poll()`? - -Use `.peek()` when you want to inspect the next element without removing it from the queue. This is useful for conditional processing, monitoring systems, or when you need to check what's next before deciding whether to process it. - -### 3. Can `.peek()` be used with all Queue implementations? - -Yes, `.peek()` is defined in the Queue interface and is available in all implementations including LinkedList, ArrayDeque, and PriorityQueue. However, the ordering of elements may differ based on the implementation (e.g., PriorityQueue orders by priority, not insertion order). From af92e65fdcc815725401cc4a632f3edb18457c75 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Wed, 6 Aug 2025 12:41:04 +0530 Subject: [PATCH 3/3] Minor changes --- .../java/concepts/queue/terms/peek/peek.md | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/content/java/concepts/queue/terms/peek/peek.md b/content/java/concepts/queue/terms/peek/peek.md index ea75f8445ea..1ace4046c7b 100644 --- a/content/java/concepts/queue/terms/peek/peek.md +++ b/content/java/concepts/queue/terms/peek/peek.md @@ -9,13 +9,12 @@ Tags: - 'Collections' - 'Data Structures' - 'Methods' - - 'Queues' CatalogContent: - 'learn-java' - 'paths/computer-science' --- -The **`.peek()`** method retrieves the head element of a [Queue](https://www.codecademy.com/resources/docs/java/queue) without removing it from the queue. If the queue is empty, it returns `null` instead of throwing an exception. This makes it a safe way to check what element is next in line without modifying the queue structure. +In Java, the **`.peek()`** method retrieves the head element of a queue without removing it from the queue. If the queue has no elements, it returns `null` instead of throwing an exception. This makes it a safe way to check what element is next in line without modifying the queue structure. ## Syntax @@ -23,11 +22,11 @@ The **`.peek()`** method retrieves the head element of a [Queue](https://www.cod queueName.peek() ``` -**Return value:** +**Return value:** Retrieves the head of the queue without removing it. Returns `null` if the queue is empty. -## Example 1: Usage of `.peek()` Method +## Example 1: Basic Usage of `.peek()` This example demonstrates how `.peek()` is used to inspect the next element without removing it from the queue: @@ -39,22 +38,22 @@ public class Main { public static void main(String[] args) { // Create a queue for customer service Queue customerQueue = new LinkedList<>(); - + // Add customers to the queue customerQueue.offer("Alice"); customerQueue.offer("Bob"); customerQueue.offer("Charlie"); - + System.out.println("Queue: " + customerQueue); - + // Peek at the next customer without removing them String nextCustomer = customerQueue.peek(); System.out.println("Next customer to be served: " + nextCustomer); - + // Queue remains unchanged after peek System.out.println("Queue after peek: " + customerQueue); System.out.println("Queue size: " + customerQueue.size()); - + // Serve the customer String servedCustomer = customerQueue.poll(); System.out.println("Served customer: " + servedCustomer); @@ -87,28 +86,28 @@ import java.util.LinkedList; public class EmptyQueueHandling { public static void main(String[] args) { Queue messageQueue = new LinkedList<>(); - + // Peek at empty queue String result = messageQueue.peek(); System.out.println("Peek on empty queue: " + result); System.out.println("Queue is empty: " + messageQueue.isEmpty()); - + // Add some messages messageQueue.offer("Welcome message"); messageQueue.offer("Alert notification"); - + // Peek at non-empty queue String nextMessage = messageQueue.peek(); System.out.println("Next message: " + nextMessage); - + // Process all messages while checking what's next while (!messageQueue.isEmpty()) { String current = messageQueue.peek(); System.out.println("About to process: " + current); - + String processed = messageQueue.poll(); System.out.println("Processed: " + processed); - + // Check if there are more messages String upcoming = messageQueue.peek(); if (upcoming != null) {