简体   繁体   中英

What is the difference between a collection of associations and a dictionary in Smalltalk?

| dict |
dict := #{'foo'->'brown'. 'bar'->'yellow'.
          'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
dict at: 'qix'

If I PrintIt , I get 'white'. If I remove 'asDictionary', I still get 'white'. What does a dictionary give me that a collection of associations doesn't?

Expression like #{exp1 . sxp2 . exp3} #{exp1 . sxp2 . exp3} #{exp1 . sxp2 . exp3} is specific and creates a HashedCollection , which is a special kind of dictionary where keys are strings (probably in Javascript you use things like this a lot).

In other smalltalks there is no expression like that. Instead array expressions which look like: {exp1 . sxp2 . exp3} {exp1 . sxp2 . exp3} {exp1 . sxp2 . exp3} (there is no leading # ) were introduced in and are also available in (which is a fork of Squeak) and Amber. Now the array expression creates an Array and so you have to use integers for #at: message. For example dict at: 2 will return you an association 'bar'->'yellow' because it is on the second position of the array you've created.

#asDictionary is a method of a collection that converts it into a dictionary given that the elements of the collection are associations. So if you want to create a dictionary with keys other than strings, you can do it like this:

dict := {
    'foo' -> 'brown'  .
        1 -> 'yellow' .
    3 @ 4 -> 'white'  .
   #(1 2) -> 'red' } asDictionary

A Dictionary is a collection of Association s. It is, in fact, Smalltalk's canonical collection of Associations. (An instance of the Association Class is a key value pair, where the value can be an object of any Class).

The advantage a Dictionary gives you is that it has specialised methods for dealing with Associations, compared to other Collections you might be tempted to use.

A Dictionary provides:
removeKey: aKey . removes aKey
includesKey: aKey . checks for the existence of the key
includes: aValue . checks for the existence of a value
at:put: . shorthand for

anAssociation := Association key:value:   .
aDictionary add:    

eg

anAssociation := Association key:   'Hello' 
                             value: 'A greeting people often use' .
aDictionary add: anAssociation .

If the key already exists in the Dictionary, then at:put will overwrite the pre-existing value with the new value, so it's important to check and make sure that the key has a unique value when adding new items.

Both the key and the value can be an object instance of any Class. Every Association in a Dictionary can be any kind of object, and every single key and value might be a instance of a different Class of object from every other element in the Dictionary.

You can create an Association by

anAssociation := Association key: 'keyOfElement' value: 'valueOfElement'  

or, more succinctly,

anAssociation := 'keyOfElement' -> 'valueOfElement'  

If you want to use keys entirely made specifically of Symbol s, there is also the Class IdentityDictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM