Previous Table of Contents Next


Generally, collections are heterogeneous. The elements they store may be any class of object and the elements of an individual collection may be of different classes. ByteArray, Interval, and String are exceptions in that their elements are restricted to types of objects.

All types of collections except intervals may be created using the protocol named <collection class>:

Protocol <collection class> Conforms to: <Object class>
Messages:
new: <integer> ^ <collection> Creates a new collection big enough to hold the specified number of elements

The message new: sent to a collection class object creates a new instance of the class that is capable of holding at least the number of elements specified by the argument. Here’s an example:

  a := Array new: 10.
  s := Set new: 256.
  t := SortedCollection new:15.
  a size
      [special character]10
  t size
      [special character]0

Instances of Array, ByteArray, and String have a fixed size that is specified when the collection is created. The array created in the above expression will have exactly 10 elements, each of which will initially contain the value nil. Most other types of collections may be dynamically sized. For these collections, the message new: creates an empty collection. That is why the message size sent to t in the above expression returns 0. The newly created SortedCollection is empty. For this type of collection the argument to new: is used to specify an anticipated number of elements that will be stored in the collection. This may be used to optimize the internal representation of the collection instance.

Because <collection class> conforms to <Object class>, objects that support this protocol also implement the message new. When the message new is sent to a collection class object, a new empty collection is created. Instances of Array, ByteArray, and String have a fixed size of zero elements:

  OrderedCollection new.
  Dictionary new.
  String new.    “A null String has not elements”

All types of collection class objects except for dictionaries and intervals also implement the protocol <initializeableCollection class>. This protocol provides messages that create new collection instances that are initialized to contain specific elements:

Protocol <initializeableCollection class> Conforms to: <collection class>
Messages:
with: <Object>^<collection> Creates a new collection initialized with 1 element
with:<Object>with:<Object>^<collection> Creates a new collection initialized with 2 elements
with:<Object>with:<Object>with:<Object>^<collection> Creates a new collection initialized with 3 elements
with: <Object> with: <Object> with: <Object>with: <Object> with: <Object> ^ <collection> Creates a new collection initialized with 4 elements
withAll: <collection> ^ <collection> Creates a new collection initialized with the elements of the argument collection

The with: messages create new collections that are instances of the receiver class and that contain the arguments as their elements. Here’s an example:

  a := Array with: Customer new.
  o := OrderedCollection with: ‘abc’ with: ‘def’.

The message withAll: creates a new collection containing the same elements as an argument collection. The class of the argument collection need not be the same as that of the new collection:

  noDups := Set withAll: #(‘xyz’ ‘abc’ ‘qwer’ ‘abc’).
  ordered := SortedCollection withAll: noDups.
  Array withAll: ordered
      #(‘abc’ ‘qwer’ xyz’)

This example uses withAll: messages along with different collection classes to produce an ordered array of strings with duplicates eliminated. The first line creates a Set whose contents is initialized from a literal array of string objects. Because instances of Set do not have duplicate elements one of the ‘abc’ strings is discarded. The second line creates a SortedCollection from the contents of the Set. The SortedCollection uses the default sort function, which sorts the strings into ascending order. Finally, the last expression creates an array whose contents are the sorted strings from the SortedCollection.

Dictionaries do not implement the <initializeableCollection class> protocol because they require the association of a key with each element. <SortedCollection class> extents the protocol of <initializeableCollection class> to allow the explicit specification of a sort function for the collection.

Protocol <SortedCollection class> Conforms to: <initializeableCollection class>
Messages:
sortBlock: <block2> ^ <SortedCollection> Creates an empty SortedCollection that uses the specified sort block

The message sortBlock: is used to specify a function for ordering elements as they are added to the collection. The function is specified using a block, called a sort block. The block must be a two-argument block that returns a boolean object. For example, a SortedCollection that orders its elements in descending order of their sizes would be created using

  SortedCollection sortBlock: [:e1 :e2| e1 size > e2 size]

A sort block should be defined such that it returns true if the object passed as its first argument should come before the argument passed as the second argument. If a SortedCollection is created without the explicit specification of a sort block, then the following default sort block is used:

  [:e1 :e2| e1 < e2]


Previous Table of Contents Next