Dart Set elementAt()
Syntax & Examples


Set.elementAt() method

The `elementAt` method in Dart returns the element at the specified index in the set.


Syntax of Set.elementAt()

The syntax of Set.elementAt() method is:

 E elementAt(int index) 

This elementAt() method of Set returns the indexth element.

Parameters

ParameterOptional/RequiredDescription
indexrequiredThe index of the element to retrieve from the set.

Return Type

Set.elementAt() returns value of type E.



✐ Examples

1 Retrieve element from integer set

In this example,

  1. We create a Set set containing integers.
  2. We use the elementAt() method to retrieve the element at index 2.
  3. We print the retrieved element to standard output.

Dart Program

void main() {
  Set<int> set = {1, 2, 3, 4, 5};
  int element = set.elementAt(2);
  print('Element at index 2: $element');
}

Output

Element at index 2: 3

2 Retrieve element from string set

In this example,

  1. We create a Set set containing strings.
  2. We use the elementAt() method to retrieve the element at index 1.
  3. We print the retrieved element to standard output.

Dart Program

void main() {
  Set<String> set = {'apple', 'banana', 'orange'};
  String element = set.elementAt(1);
  print('Element at index 1: $element');
}

Output

Element at index 1: banana

Summary

In this Dart tutorial, we learned about elementAt() method of Set: the syntax and few working examples with output and detailed explanation for each example.