29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

| temp |<br />

temp := Set new.<br />

temp add: 'John'.<br />

temp add: 'Paul'.<br />

temp add: 'Peter'.<br />

temp add: 'John'.<br />

temp inspect.<br />

You will find that only one occurrence of the string ‘ John’ was added to the set. The second<br />

addition was ignored.<br />

There is also a version of the collection class Set called IdentitySet. This type of set performs<br />

<strong>an</strong> identity test to determine if <strong>an</strong> object is a duplicate or not. That is, it uses the == test rather th<strong>an</strong> the =<br />

test. This me<strong>an</strong>s that rather th<strong>an</strong> <strong>an</strong> object having the same value as <strong>an</strong> object already in a se t, it must<br />

actually be that object. If it is only equivalent then the object will be added to the set. In this way it may<br />

appear that duplicates have been added to the set. However, they are not the same object <strong><strong>an</strong>d</strong> are<br />

therefore allowed.<br />

The IdentitySet c<strong>an</strong> be useful if you know that duplicate elements to be added to the set will be<br />

the same object. In such cases the Identity set c<strong>an</strong> be used as it is more efficient th<strong>an</strong> the st<strong><strong>an</strong>d</strong>ard set.<br />

10.7 OrderedCollection<br />

The OrderedCollection cla ss c<strong>an</strong> hold <strong>an</strong>y type of object. It does so in a specified order. That<br />

order is determined by the order in which objects are added to the OrderedCollection inst<strong>an</strong>ce. An<br />

inst<strong>an</strong>ce of OrderedCollection is created by using either new or new:. The second version of new<br />

allows <strong>an</strong> integer to be provided which indicates the maximum size of the collection required. This c<strong>an</strong><br />

be useful in situations where this number is known <strong><strong>an</strong>d</strong> time is optimal. That is, it is not necessary to<br />

waste time incrementally growing the collection.<br />

OrderedCollection c<strong>an</strong> be used in situations where the order in which the objects were added<br />

to the inst<strong>an</strong>ce must be preserved. For example, try out the following example of using <strong>an</strong><br />

OrderedCollection:<br />

| x |<br />

x := OrderedCollection new.<br />

x add: 'one'.<br />

x add: 'two'.<br />

x add: 'three'.<br />

x inspect.<br />

You will find that the strings ‘one’, ‘two’ <strong><strong>an</strong>d</strong> ‘three’ remain in the order in which they were added<br />

(unlike inst<strong>an</strong>ces of Bag <strong><strong>an</strong>d</strong> Set).<br />

There is a wide r<strong>an</strong>ge of order related messages which allow obj ects to be added <strong><strong>an</strong>d</strong> accessed with<br />

reference to the order in the OrderedCollection inst<strong>an</strong>ce. For example, it is possible to access <strong>an</strong><br />

object “before” or “after” <strong>an</strong>other object by using the before: or after: messages, e.g.:<br />

x after: 'two'.<br />

It is also possible to add <strong>an</strong> object either before or after <strong>an</strong>other object. For example:<br />

x add: 'five' after: 'one'.<br />

x add: 'six' before: 'three'.<br />

To remove <strong>an</strong> object from the collection use remove:ifAbsent:, this removes t he first inst<strong>an</strong>ce<br />

of the object specified in the collection. If the object is not present, then the absent block will be<br />

executed. For example:<br />

a remove: 'John' ifAbsent: [Dialog warn: 'John not present'].<br />

91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!