Choose a collection¶
Collections hold several values. Choose the type based on how you will read and change those values.
Import: import system.collections;
| You need… | Use |
|---|---|
| Items you can read by position | List |
| Values you look up by a key | Map |
| Keys kept in sorted order | SortedMap |
| Unique values | Set |
| Items processed in arrival order | Queue |
| The most recently added item first | Stack |
| The smallest or highest-priority item first | PriorityQueue |
| Filtering and transforming items | Queries and Seq |
Guides in this section¶
- ListA growable sequence you read and change by position.
- MapLook up values by key.
- SortedMapA map that keeps its keys in order.
- SetKeep unique values and test membership.
- QueueProcess items in the order they arrive.
- StackRemove the most recently added item first.
- PriorityQueueRemove the smallest or highest-priority item first.
- Queries and SeqFilter, transform, and combine collection items.
Start with a list¶
import system;
import system.collections;
fun main() {
let names: List<string> = ["Ada", "Sam"];
names.push("Lee");
for (let name in names) {
System.println(name);
}
}
This prints each name on a separate line. The List<string> type tells Dream to make a growable list. Without that expected type, square brackets create a fixed array.
Typed {value, value} creates a set, and typed {key: value} creates a map. Empty collections need a type, such as let names: Set<string> = {};.
Missing items and positions¶
Check each collection's result type before assuming an item exists. Map lookups and queue removals can return None. List indexing uses a position and stops the program if that position is outside the list.
See Option and Result for handling absence and the API catalog for all public members.