From 121730df63822ff2163a5f4a0dcaa3917fef8a5b Mon Sep 17 00:00:00 2001 From: ShaMethyste Date: Wed, 28 Jan 2026 11:41:18 +0100 Subject: [PATCH 1/2] add of the directory .retainWhere and of the file retainwhere.md --- .../queue/terms/retainWhere/retainWhere.md | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 content/dart/concepts/queue/terms/retainWhere/retainWhere.md diff --git a/content/dart/concepts/queue/terms/retainWhere/retainWhere.md b/content/dart/concepts/queue/terms/retainWhere/retainWhere.md new file mode 100644 index 00000000000..527c95dfd01 --- /dev/null +++ b/content/dart/concepts/queue/terms/retainWhere/retainWhere.md @@ -0,0 +1,52 @@ +--- +Title: '.retainWhere' +Description: 'Remove all the elements that fail to satisfy a test in a queue.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Dart' + - 'Queues' + - 'Properties' +CatalogContent: + - 'learn-dart' + - 'paths/computer-science' +--- + +In Dart, the **`.length`** property removes all the elements that fail to satisfy a test in a queue. + +## Syntax + +```pseudo +queueName.retainWhere() +``` + +- `queueName`: The name of the queue to be checked. + +## Example + +The below example creates a queue named `example_queue`, appends five integer elements to it, and checks if any of its strictly superior to 4 using the `.retainWhere ()` property: + +```dart +import 'dart:convert'; +import 'dart:collection'; + +void main() +{ + // Initializing a queue + Queue example_queue = Queue(); + + // Appending elements to the queue + example_queue.addAll([1, 3, 5, 2, 0]); + + // Check if any element it's superior to 4 + example_queue.retainWhere((element) => element < 4); + print("The queue with the correct numbers is: ${example_queue.retainWhere()}"); +} +``` + +The above code will give the following output: + +```shell +The queue with the correct numbers is: 1, 3, 2, 0 +``` From 388b44724f33c75cd33123885a3ba51e917216e9 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 29 Jan 2026 18:04:14 +0530 Subject: [PATCH 2/2] Revise .retainWhere() documentation for clarity Updated the documentation for the .retainWhere() method to clarify its functionality and parameters. Improved examples and syntax formatting. --- .../queue/terms/retainWhere/retainWhere.md | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/content/dart/concepts/queue/terms/retainWhere/retainWhere.md b/content/dart/concepts/queue/terms/retainWhere/retainWhere.md index 527c95dfd01..37e100dfb02 100644 --- a/content/dart/concepts/queue/terms/retainWhere/retainWhere.md +++ b/content/dart/concepts/queue/terms/retainWhere/retainWhere.md @@ -1,52 +1,55 @@ --- -Title: '.retainWhere' -Description: 'Remove all the elements that fail to satisfy a test in a queue.' +Title: '.retainWhere()' +Description: 'Retains only the elements that satisfy a given condition.' Subjects: - 'Computer Science' - 'Code Foundations' Tags: - 'Dart' + - 'Methods' - 'Queues' - - 'Properties' CatalogContent: - 'learn-dart' - 'paths/computer-science' --- -In Dart, the **`.length`** property removes all the elements that fail to satisfy a test in a queue. +The **`.retainWhere()`** method removes all elements from a `Queue` that do not satisfy the given condition, keeping only the elements for which the condition returns `true`. ## Syntax ```pseudo -queueName.retainWhere() +queue.retainWhere(test) ``` -- `queueName`: The name of the queue to be checked. +**Parameters:** -## Example +- `test` (bool Function(E element)): A function that returns `true` for elements that should be retained in the queue. -The below example creates a queue named `example_queue`, appends five integer elements to it, and checks if any of its strictly superior to 4 using the `.retainWhere ()` property: +**Return value:** + +Returns `void`. The original queue is updated by retaining only the matching elements. + +## Example: Retaining elements in a queue based on a condition + +In this example, a queue is created and populated with integers. The `.retainWhere()` method is then used to keep only the elements that are less than 4: ```dart -import 'dart:convert'; import 'dart:collection'; -void main() -{ - // Initializing a queue +void main() { Queue example_queue = Queue(); - // Appending elements to the queue example_queue.addAll([1, 3, 5, 2, 0]); - // Check if any element it's superior to 4 + // Keep elements less than 4 example_queue.retainWhere((element) => element < 4); - print("The queue with the correct numbers is: ${example_queue.retainWhere()}"); + + print("The queue with the correct numbers is: $example_queue"); } ``` The above code will give the following output: ```shell -The queue with the correct numbers is: 1, 3, 2, 0 +The queue with the correct numbers is: {1, 3, 2, 0} ```