Dart Set lookup()
Syntax & Examples
Syntax of Set.lookup()
The syntax of Set.lookup() method is:
E lookup(Object object) This lookup() method of Set if an object equal to object is in the set, return it.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
object | required | The object to look up in the set. |
Return Type
Set.lookup() returns value of type E.
✐ Examples
1 Looking up integer in Set
In this example,
- We create a Set
numSetwith values {1, 2, 3}. - We set
valueToFindto 2. - We use the
lookup()method to findvalueToFindinnumSet. - We print the found value, if any.
Dart Program
void main() {
Set<int> numSet = {1, 2, 3};
int valueToFind = 2;
int? foundValue = numSet.lookup(valueToFind);
print('Found value: $foundValue');
}Output
Found value: 2
2 Looking up string in Set
In this example,
- We create a Set
wordSetwith values {'apple', 'banana', 'cherry'}. - We set
wordToFindto 'banana'. - We use the
lookup()method to findwordToFindinwordSet. - We print the found word, if any.
Dart Program
void main() {
Set<String> wordSet = {'apple', 'banana', 'cherry'};
String wordToFind = 'banana';
String? foundWord = wordSet.lookup(wordToFind);
print('Found word: $foundWord');
}Output
Found word: banana
Summary
In this Dart tutorial, we learned about lookup() method of Set: the syntax and few working examples with output and detailed explanation for each example.