Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.
ListQueue
is a list based onQueue
. Read more aboutListQueue
here.
The firstWhere
method will loop through all the elements of the queue in the iteration order and check each element against the test condition. It will return the first element that satisfies the provided predicate function.
E firstWhere(bool test(E el),{E orElse()?})
This method takes two arguments
test
: This is a orElse
: This is a function that will be invoked when no element of the queue satisfies the passed test
predicate function. The value returned from this orElse
method is returned as the return value of the firstWhere
method. Note that this is an optional argument.This method returns the first element that satisfies the provided test
condition. If no element satisfies the condition, the orElse
function argument is invoked and the value returned from that function is returned.
The code below demonstrates how to use the firstWhere
method:
import 'dart:collection';void main() {// create a queueListQueue queue = new ListQueue();// add 3 elements to the queuequeue.add(10);queue.add(20);queue.add(30);print('The queue is : $queue');// Get the first element which is greater than 10var result = queue.firstWhere( (e)=> e > 10 );print('The first element which is greater than 10 : $result');// Get the first element which is less than 0result = queue.firstWhere( (e)=> e < 0, orElse: ()=> null);print('The first element which is less than 0: $result');}
In the code above:
In line 1, we import the collection
library.
In line 4, we create a ListQueue
with the name queue
.
In lines 7-9, we use the add
method to add three elements 10, 20, 30 to the queue. Now the queue is {10,20,30}.
In line 13, we use the firstWhere
method with a predicate function. The predicate function will check if the element is greater than 10. In our case, the element 20
is the first element that is greater than 10. So, 20
is returned as the result.
In line 17, we use the firstWhere
method with a predicate function and an orElse
function. The predicate function will check if the element is negative. In our case, all elements of the queue are positive so the orElse
method is invoked and null
is returned from the orElse
method.
RELATED TAGS
CONTRIBUTOR
Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.