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

ParameterOptional/RequiredDescription
objectrequiredThe 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,

  1. We create a Set numSet with values {1, 2, 3}.
  2. We set valueToFind to 2.
  3. We use the lookup() method to find valueToFind in numSet.
  4. 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,

  1. We create a Set wordSet with values {'apple', 'banana', 'cherry'}.
  2. We set wordToFind to 'banana'.
  3. We use the lookup() method to find wordToFind in wordSet.
  4. 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.