[英]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'. 如果我
PrintIt
,我会PrintIt
'白色'。 If I remove 'asDictionary', I still get 'white'. 如果我删除'asDictionary',我仍然会'白'。 What does a dictionary give me that a collection of associations doesn't?
字典给了我一些关联的集合不是什么?
Expression like #{exp1 . sxp2 . exp3}
表达式如
#{exp1 . sxp2 . exp3}
#{exp1 . sxp2 . exp3}
#{exp1 . sxp2 . exp3}
is amber-smalltalk 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). #{exp1 . sxp2 . exp3}
是特定于amber-smalltalk并创建一个HashedCollection ,这是一种特殊的字典,其中键是字符串(可能在Javascript中你经常使用这样的东西)。
In other smalltalks there is no expression like that. 在其他小方面,没有这样的表达。 Instead array expressions which look like:
{exp1 . sxp2 . exp3}
而是数组表达式,如:
{exp1 . sxp2 . exp3}
{exp1 . sxp2 . exp3}
{exp1 . sxp2 . exp3}
(there is no leading #
) were introduced in squeak and are also available in pharo (which is a fork of Squeak) and Amber. {exp1 . sxp2 . exp3}
(没有前导#
)是在吱吱声中引入的,也有pharo (这是Squeak的一个分支)和Amber。 Now the array expression creates an Array and so you have to use integers for #at:
message. 现在数组表达式创建了一个数组 ,所以你必须为
#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. 例如,
dict at: 2
将返回一个关联'bar'->'yellow'
因为它位于你创建的数组的第二个位置。
#asDictionary
is a method of a collection that converts it into a dictionary given that the elements of the collection are associations. #asDictionary
是一个集合的方法,只要集合的元素是关联,它就会将其转换为字典。 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. Dictionary
是 Association
的集合。 It is, in fact, Smalltalk's canonical collection of Associations. 事实上,它是Smalltalk的规范协会集合。 (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. Dictionary为您提供的优势在于,与您可能想要使用的其他集合相比,它具有处理关联的专用方法。
A Dictionary
provides: Dictionary
提供:
removeKey: aKey .
removes aKey 删除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. 如果密钥已存在于Dictionary中,则
at:put
将使用新值覆盖预先存在的值,因此检查并确保在添加新项时密钥具有唯一值非常重要。
Both the key and the value can be an object instance of any Class. 键和值都可以是任何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.
字典中的每个关联都可以是任何类型的对象,并且每个键和值可以是与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
如果您想使用完全由
Symbol
的密钥,那么还有Class
IdentityDictionary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.