CodeBlanks Logo Icon CodeBlanks

Summary - Collections

Combine List, Set and Map to write a phone book.

Summary - Collections

In this group we learned four core collections: List is ordered and allows duplicates; Set holds unique elements; Map stores key-value pairs; Queue works in FIFO order. Their common ancestor is Iterable.

Each collection fits its scenario. List for ordered data, Set for deduplication, Map for fast lookup, Queue for ordered processing. Iterable offers lazy traversal.

Now let's do an exercise that uses them together: create three different collections and print their lengths.

Practice Create three collections

Create a List<int> (3 elements), a Set<int> (2 elements) and a Map<String, int> (2 pairs). Print the length of each on a separate line.

Starter code

main.dart
void main() {
  // create a List, a Set and a Map
  // print the length of each
}

Sample solution

main.dart
void main() {
  var list = <int>[1, 2, 3];
  var set = <int>{4, 5};
  var map = <String, int>{'a': 1, 'b': 2};
  print(list.length);
  print(set.length);
  print(map.length);
}

Expected output

3
2
2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match collections to their purpose

Term Meaning
List Ordered, duplicates
Set Unique elements
Map Key-value
Queue FIFO order

2 Which collection stores key-value pairs?

  • List
  • Map Correct answer
  • Queue
  • Iterable

Explanation: Map stores key-value pairs.

All lessons in Intermediate