Previous | Table of Contents | Next |
Elements are normally added to a dictionary using the message at:put:. The argument for the at: keyword is the key, and the argument for the put: keyword is the element that is associated with the key. Elements are retrieved using the at: message and a key. The message includesKey: can be used to test whether the collection includes an element with a specific key. Elements are removed using removeKey::
d := Dictionary new. obj := Object new. d at: abc put: xyz. keys and value may be any class of objects d at: 123 put: #(one two three). d at: obj: put: [self doIt]. even blocks may be stored s size 3 d includes: obj true d includes: Object new false not the same key as used earlier d removeKey: obj. d size 2
Standard Smalltalk defines two specific classes of dictionaries: Dictionary and IdentityDictionary. Their behavior differs only in the manner in which they compare keys. An instance of Dictionary considers two objects to be the same key if an = comparison of the objects returns true. An instance of IdentityDictionary uses == to compare keys:
normalDictionary := Dictionary new. identityDictionary := IdentityDictionary new. identityDictionary at: 1 put: integer; at: 1.0 put: float; at: 1s put: scaled. identityDictionary size 3 normalDictionary addAll: identityDictionary. normalDictionary size 1
In this example, three different number objects, each with a value equal to 1, are first used as keys to IdentityDictionary. Because IdentityDictionary uses object identity to distinguish keys, the three objects are three different keys. IdentityDictionary has three elements. These keys and elements are then added to an instance of Dictionary that uses equality to distinguish keys. Because the three numeric objects all compare as equal to one another, they are considered the same key. Each element that is added replaces the preceding element with the same key, and ultimately the dictionary contains only one element.
Whereas dictionaries are unordered and their elements are accessed using arbitrary objects as keys, several other types of collections impose an ordering on their elements and allow access to elements by their ordinal position. In essence, the integer ordinal position, or index, of an element serves as a key for accessing the element. Such collections are sometimes referred to as indexable collections. The root protocol for indexable collections is named <sequencedReadableCollection>. Several commonly used classes including arrays and strings support this protocol:
Protocol <sequencedReadableCollection> abbreviated <sRC> | Conforms to: <collection> |
Messages: | |
<sRC> ^ <sRC> | Concatenates collections |
after: <Object> ^ <Object> | Returns the element that follows the argument element |
at: <integer> ^ <Object> | Returns the element at the position specified by the argument |
before: <Object> ^ <Object> | Returns the element that precedes the argument element |
copyFrom: <integer> to: <integer> ^ <sRC> | Creates a new collection from a subsequence of elements |
copyReplaceAll: <sRC> with: <sRC> ^ <sRC> | Creates a new collection with one subsequence replaced by another |
copyReplaceFrom: <integer> to: <integer> with: <sRC> ^ <sRC> | Creates a new collection from the receiver with a subsequence of elements replaced from another collection |
copyReplaceFrom: <integer> to: <integer withObject: <Object> ^ <sRC> | Creates a new collection from the receiver with a subsequence of elements replaced with a specific object |
copyWith: <Object> ^ <sRC> | Copies receiver with the argument appended |
copyWithout: <Object> ^ <sRC> | Copies receiver without any elements equal to the argument |
findFirst: <block1> ^ index | Index of first element for which the block evaluates to true |
findLast: <block1> ^ index | Index of last element for which the block evaluates to true |
first ^ <Object> | Retrieves the first element of the collection |
indexOf: <Object> ^ <number> | Index of first element equal to argument |
indexOf: <Object> ifAbsent: <block0> ^ <number> | Index of first element equal to argument; evaluates block if none |
indexOfSubCollection: <sRC> startingAt: <integer> ^ <number> | Starting at the specified position find the index of the first subsequence that is equal to the argument collection |
indexOfSubCollection: <sRC> startingAt: <integer> ifAbsent: <block0> ^ <number> | Starting at the specified position find the index of the first subsequence that is equal to the argument collection; if not found, evaluates the argument block |
last ^ <Object> | Retrieves the last element of the collection |
reverse ^ <sRC> | Creates a new collection with the receivers elements reversed |
reverseDo: <block1> | In reverse order, evaluates the block for each receiver element |
with: <sRC> do: <block2> | Evaluates the block using pairs of elements from the receiver and argument collections |
The basic message for accessing an element according to its position is at:. The numbering of element position starts at 1:
#(alpha beta gamma) at: 2 beta abcdefghijklmnopqrstuvwxyz at: 14 $n
Previous | Table of Contents | Next |