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
numSet
with values {1, 2, 3}. - We set
valueToFind
to 2. - We use the
lookup()
method to findvalueToFind
innumSet
. - 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
wordSet
with values {'apple', 'banana', 'cherry'}. - We set
wordToFind
to 'banana'. - We use the
lookup()
method to findwordToFind
inwordSet
. - 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.