Lists, sets and maps in Dart
The three core collections, each with its own literal.
void main() {
// Lists index, sets deduplicate, maps pair keys with values.
final route = ['gate', 'boardwalk', 'tower'];
print(route[1]);
final ringed = ['wren', 'robin', 'wren'].toSet();
print('${ringed.length} distinct ringed species');
final perchCounts = {'oak': 4, 'fence': 9};
perchCounts['reeds'] = 2;
print(perchCounts['fence']);
print(perchCounts.containsKey('willow'));
}
How it works
- Lists index with brackets;
route[1]is the second stop. toSet()deduplicates — three entries collapse to two.- Map literals pair keys to values; indexing a missing key gives null.
Keywords and builtins used here
finalvoid
The run, in numbers
- Lines
- 13
- Characters to type
- 384
- Tokens
- 113
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 97 seconds.
Step 1 of 3 in Collections, step 10 of 27 in Language basics.