To create a set of sets in Dart, you can use the Set
class from the Dart collections library. Each inner set can also be a Set
.
In this example,
Set
named setOfSets
, where each element is another Set
. This is done by declaring Set> setOfSets = {}
.Set set1 = {1, 2, 3}
initializes a set containing the integers 1, 2, and 3.Set set2 = {4, 5, 6}
.add
method: setOfSets.add(set1)
and setOfSets.add(set2)
.for
loop for this: for (var innerSet in setOfSets)
followed by for (var item in innerSet)
.void main() {
// Step 1: Create the outer set of sets
Set<Set<int>> setOfSets = {};
// Step 2: Create inner sets
Set<int> set1 = {1, 2, 3};
Set<int> set2 = {4, 5, 6};
// Step 3: Add inner sets to the outer set
setOfSets.add(set1);
setOfSets.add(set2);
// Step 4: Iterate and print the elements of each inner set
for (var innerSet in setOfSets) {
print('Inner set:');
for (var item in innerSet) {
print(item);
}
}
}
Inner set: 1 2 3 Inner set: 4 5 6
In this example,
Set
named setOfSets
, where each element is another Set
. This is done by declaring Set> setOfSets = {}
.Set set1 = {'apple', 'banana'}
initializes a set containing the strings 'apple' and 'banana'.Set set2 = {'cherry', 'date'}
.add
method: setOfSets.add(set1)
and setOfSets.add(set2)
.for
loop for this: for (var innerSet in setOfSets)
followed by for (var item in innerSet)
.void main() {
// Step 1: Create the outer set of sets
Set<Set<String>> setOfSets = {};
// Step 2: Create inner sets
Set<String> set1 = {'apple', 'banana'};
Set<String> set2 = {'cherry', 'date'};
// Step 3: Add inner sets to the outer set
setOfSets.add(set1);
setOfSets.add(set2);
// Step 4: Iterate and print the elements of each inner set
for (var innerSet in setOfSets) {
print('Inner set:');
for (var item in innerSet) {
print(item);
}
}
}
Inner set: apple banana Inner set: cherry date
In this tutorial, we learned How to create a Set of Sets in Dart language with well detailed examples.