How to remove and retain items from Queue in Dart with condition

How to remove and retain items from Queue with given conditions in Dart:

In this post, I will show you how to remove and retain items of a Queue in Dart by using a specific condition. The Queue class provides two methods to remove and retain the Queue items by using a condition.

  • removeWhere and
  • retainWhere

Let’s learn how to use these methods with examples.

removeWhere:

This method takes one function and removes all the elements those are matched by the function. It is defined as:

removeWhere(bool removeFunction(E e)) -> void

The removeFunction is the function we need to pass to this method.

Example of removeWhere:

Let’s try it with an example:

import 'dart:collection';

void main() {
  var givenQueue = Queue.of([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  givenQueue.removeWhere((e) => e % 3 == 0);

  print(givenQueue);
}

In this program, the givenQueue variable holds one queue of few numbers. We are removing all the elements which are divisible by 3. It modifies the original queue.

If you run this program, it will print:

{1, 2, 4, 5, 7, 8, 10}

We can also write a separate function to check each number:

import 'dart:collection';

bool canRemove(int n) {
  return n % 3 == 0;
}

void main() {
  var givenQueue = Queue.of([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  givenQueue.removeWhere(canRemove);

  print(givenQueue);
}

Example of removeWhere with a Queue of objects:

Let’s try removeWhere with objects. The following program creates one queue of objects and remove objects based on a certain condition:

import 'dart:collection';

class Student {
  String? name;
  int? age;
  String? gender;

  Student(String name, int age, String gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }

  
  String toString() {
    return "[Name: ${this.name}, Age: ${this.age}, Gender: ${this.gender}]";
  }
}

bool isMale(Student s) {
  return s.gender == "Male";
}

void main() {
  var givenQueue = Queue.of([Student("Alex", 10, "Male"), Student("Daisy", 14, "Female"), Student("Bob", 9, "Male")]);
  print("Given queue: ${givenQueue}");
  
  givenQueue.removeWhere(isMale);
  print("Modified queue: ${givenQueue}");
}

In this program,

  • We created a class Student to hold the name, age and gender of a student.
  • The givenQueue is a Queue object of three different Student objects.
  • The removeWhere is removing all Male students from the queue.

If you run this program, it will print: Dart remove items with condition

retainWhere:

The retainWhere method is similar to removeWhere. It takes one function and removes all items from the queue for those the function returns false. It is opposite to removeWhere.

Let’s try this method with an example:

import 'dart:collection';

class Car {
  String? color;

  Car(String color) {
    this.color = color;
  }

  
  String toString() {
    return "[Car color: ${this.color}]";
  }
}

bool isRedOrWhite(Car c) {
  return c.color == "Red" || c.color == "White";
}

void main() {
  var givenQueue = Queue.of(
      [Car("Red"), Car("Blue"), Car("Green"), Car("Black"), Car("White")]);
  print("Given queue: ${givenQueue}");

  givenQueue.retainWhere(isRedOrWhite);
  print("Modified queue: ${givenQueue}");
}
  • The Car can hold the color of a car.
  • We created one Queue with five different Car objects.
  • The retainWhere is using the isRedOrWhite function to retain only cars with Red or White color.

If you run this program, it will print:

Given queue: {[Car color: Red], [Car color: Blue], [Car color: Green], [Car color: Black], [Car color: White]}
Modified queue: {[Car color: Red], [Car color: White]}

Refer:

You might also like: