The dart:collection
library provides advanced collection support for the Dart language. It also contains the DoubleLinkedQueue<E>
class which implements the abstract Queue<E>
class, using a doubly-linked list.
contains()
methodThe DoubleLinkedQueue<E>
class contains the contains()
method which checks the presence of an element in the queue.
A queue is a FIFO (First In First Out) data structure. In a queue, the element which is added first will be deleted first.
The contains()
method does not change the state of the queue.
The illustration below shows how the contains()
method works.
The prototype of the contains()
method is shown below.
bool contains(
Object? element
)
The contains()
method takes the object to search in the queue as the argument.
contains()
method returns a Boolean value. It returns true
if the input element is present in the queue and false
otherwise.contains()
method is a O(n) operation where n is the number of the elements in the queue.The code below shows how the contains()
method can be used in Dart.
import 'dart:collection';void main() {var faangCompanies = DoubleLinkedQueue<String>();faangCompanies.add("Facebook");faangCompanies.add("Apple");faangCompanies.add("Amazon");faangCompanies.add("Netflix");faangCompanies.add("Google");print('Printing Queue Elements');print(faangCompanies);bool isGooglePresent = faangCompanies.contains("Google");print('Checking presence of "Google" using contains(): ${isGooglePresent}');bool isMicrosoftPresent = faangCompanies.contains("Microsoft");print('Checking presence of "Microsoft" using contains(): ${isMicrosoftPresent}');}
The code above performs the following actions:
An instance of the DoubleLinkedQueue
class of type String
is initialized in line 3.
Next, a few strings, "Facebook"
, "Apple"
, "Amazon"
, "Netflix"
, and "Google"
, are added to the queue using the add()
method.
The queue elements are displayed as {Facebook, Apple, Amazon, Netflix, Google}
. All the elements are added to the end of the queue, as the queue is a FIFO data structure.
The contains()
method in line 13 checks for the presence of "Google"
in the queue and stores the boolean result in a variable. It is true
because "Google"
is present in the queue and is printed in the next line.
The contains()
method in line 16 checks for the presence of "Microsoft"
in the queue and stores the boolean result in a variable. It is false
because "Microsoft"
is not present in the queue and is printed in the next line.