Friday, 30 October 2015

Sets and Dictionaries - OO Computing with Smalltalk - Ch 23 original notes

Objects which comprise a collection are referred to as elements

A collection may have elements which are not the same class as the other elements - but this is not common

Ordered and sorted
An ordered collection preserves the order in which the elements were added to the collection

A sorted collection has an ordering determined by a property of the element.

Only one element exists with a given value

For all collections:
add: and return: returns the argument, not the modified receiver

both have ifAbsent: variants - add:IfAbsent: and remove:ifAbsent:

ifAbsent: catches exceptions Or you can pre-qualify the add: by checking if the collection already includes: the element to be added

A key is a property that uniquely identifies an element.

An Association is a key value pair

When creating instances of Association, it's important to check that the key is unique.

A collection of Association objects

Create an association by
    anAssociation := Association key: 'keyOfElement' value: 'valueOfElement'
    anAssociation := ( 'keyOfElement' -> 'valueOfElement' )

A Dictionary is not allowed to remove one of its associations directly, so there are overridden inherited methods remove: and remove:ifAbsent: which cause an exception if used.

Instead, removeKey: aKey is used.

An includesKey: message checks for the existence of the key. includes:checks for the existence of a value.

at:ifAbsent: is needed - for what?

at:put: combines  anAssociation := Association key:value: with aDictionary add: anAssociation

if the key already exists in the dictionary, then the pre-existing value is over-written with the new value.

No comments:

Post a Comment