Collections: The Hierarchy
A dictionary is conceptually a set of associations (key -> value).
20.1 Overview
Collection
├── SequenceableCollection (ordered, indexed from 1)
│ ├── ArrayedCollection (fixed size)
│ │ ├── Array #(1 2 3)
│ │ ├── String ── Symbol 'abc' #abc
│ │ ├── ByteArray #[1 2 3]
│ │ └── (Float/Word arrays, Bitmap …)
│ ├── OrderedCollection (growable list — the workhorse)
│ ├── SortedCollection (kept sorted by a sort block)
│ ├── Interval 1 to: 10 by: 2
│ └── LinkedList (of Link objects; used by the scheduler)
├── HashedCollection
│ ├── Set ── IdentitySet
│ └── Dictionary ── IdentityDictionary, SmallDictionary, (OrderedDictionary in Pharo)
└── Bag (counts occurrences)20.2 Choosing a collection
Fixed size, indexed Growable list, add/remove at ends SortedCollection (or sort on demand with sorted:) Unique elements, fast membership Key → value lookup
Keys compared by identity (symbols, objects) IdentityDictionary, IdentitySet OrderedDictionary (Pharo) String, Symbol
20.3 Array
| a |
a := Array new: 3. "→ #(nil nil nil)"
a at: 1 put: 10; at: 2 put: 20; at: 3 put: 30.
a at: 2. "→ 20"
a first. a last. a size. "→ 10 30 3"
Array with: 1 with: 2 with: 3. Array new: 5 withAll: 0.
#(1 2 3) , #(4 5). "→ #(1 2 3 4 5)"
#(1 2 3) copyWith: 4. "→ #(1 2 3 4)"
#(1 2 3 4 5) copyFrom: 2 to: 4. "→ #(2 3 4)"
a at: 4. "→ SubscriptOutOfBounds error"20.4 OrderedCollection
| oc |
oc := OrderedCollection new.
oc add: 1; add: 2; addFirst: 0; addLast: 3. "→ an OrderedCollection(0 1 2 3)"
oc addAll: #(4 5).
oc removeFirst. "→ 0"
oc removeLast. "→ 5"
oc remove: 2. "removes the element 2"
oc remove: 99 ifAbsent: [ nil ].
oc includes: 3. "→ true"
oc add: 9 beforeIndex: 1.
oc asArray.20.5 Set and Bag
| s b |
s := Set new.
s add: 1; add: 2; add: 1. "→ a Set(1 2)"
#(1 2 2 3 3 3) asSet. "→ a Set(1 2 3)"
#(1 2 3) asSet union: #(3 4) asSet.
#(1 2 3) asSet intersection: #(2 3 4).
b := Bag new.
b add: #apple; add: #apple; add: #pear.
b occurrencesOf: #apple. "→ 2"
b add: #fig withOccurrences: 5.
#(1 1 2) asBag sortedCounts. "→ {2->1. 1->2}"20.6 Dictionary
A dictionary is conceptually a set of associations (key -> value). Iterating with do: visits values.
| d |
d := Dictionary new.
d at: #apple put: 3; at: #pear put: 5.
d at: #apple. "→ 3"
d at: #kiwi. "→ KeyNotFound error"
d at: #kiwi ifAbsent: [ 0 ]. "→ 0"
d at: #kiwi ifAbsentPut: [ 7 ]. "→ 7, now stored"
d at: #apple ifPresent: [ :v | v * 10 ].
d removeKey: #pear.
d includesKey: #apple. "→ true"
d keys. d values. d associations.
d keysAndValuesDo: [ :k :v | Transcript show: k, '=', v printString; cr ].
{ #a -> 1 . #b -> 2 } asDictionary. "→ a Dictionary(#a->1 #b->2)"
d collect: [ :v | v * 2 ]. "→ dictionary with same keys, transformed values"
d select: [ :v | v > 3 ].20.7 Interval
1 to: 10. "→ (1 to: 10)"
(1 to: 10 by: 3) asArray. "→ #(1 4 7 10)"
(10 to: 1 by: -1) asArray.
(1 to: 5) collect: [ :i | i * i ]. "→ #(1 4 9 16 25)"
(0 to: 1 by: 1/4) asArray. "→ #(0 (1/4) (1/2) (3/4) 1)"20.8 SortedCollection and sorting
| sc |
sc := SortedCollection sortBlock: [ :a :b | a > b ]. "descending"
sc addAll: #(3 1 4 1 5 9 2 6).
sc first. "→ 9"
sc asArray. "→ #(9 6 5 4 3 2 1 1)"
#(3 1 2) sorted. "→ #(1 2 3) (a sorted copy)"
#(3 1 2) sorted: [ :a :b | a > b ]. "→ #(3 2 1)"
#('pear' 'fig' 'apple') sorted: [ :a :b | a size < b size ].
people sorted: #age ascending. "Pharo sort functions"
people sorted: #lastName ascending, #firstName ascending.
#(3 1 2) asOrderedCollection sort; yourself. "sort in place"20.9 LinkedList
LinkedList is a list of Link objects with constant-time insertion at either end; Pharo wraps arbitrary objects in ValueLinks automatically (LinkedList new add: 3; add: 4; addFirst: 2; yourself). It is mainly used internally (e.g., process queues in the scheduler); OrderedCollection is the usual choice.
20.10 Conversions
asArray, asOrderedCollection, asSet, asBag, asSortedCollection, and as: aClass (#($a $b) as: String → 'ab') convert between collection kinds.