Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Dictionary
Last updated at 4:49 pm UTC on 10 October 2019
Dictionary is a subclass of Set.

Subclasses are:

A #Dictionary is a type of collection populated with entries which consists of Association objects, also known as a key/value pairs. Both the key and the value can be any arbitrary object. Dictionaries are relatively fast at finding the value associated with a key. They are real work horses in Smalltalk programming. There are similar data structures like the hashes in Java and Perl.

The associations in a dictionary can be accessed by key or value. There are methods which will return a key, a value, or an association. Associations can be created in several different ways. The method -> dash right-arrow is an example of a non-alphabetic method name.


How to construct a dictionary

|a|
a:= Dictionary new.
a add:(Association key: 'Thomas' value: '55565325').
a at: 'Christoph' put: 4784582.
a add: 'Sample' -> ((Morph new)name: 'Sample').


"Inspection"
a inspectWithLabel: '1st Dictionary'.
(a at: 'Sample')  openInHand.
Transcript show: (a at: 'Christoph'); cr.
Transcript show: (a keyAtValue: '55565325').



Finding a dictionary by a key

In one workspace evaluate
dict := Dictionary new.
dict at: 'blue' put: 'bleu'.
dict at: 'red' put: 'red'.
dict at: 'green' put: 'green'


In another workspace evaluate
(Dictionary allInstances detect: [:dict | dict includesKey: 'blue']) inspect


Iterating

 myDict keysDo: [ :key | key doSomething ].
 myDict valuesDo: [ :value | value doSomething ].

Note

JsonObject is a subclass of Dictionary.