[Newbies] Dictionary and order

Matthew Fulmer tapplek at gmail.com
Fri Sep 7 15:18:12 UTC 2007


On Fri, Sep 07, 2007 at 03:30:17PM +0200, Norbert Hartl wrote:
> 
> On Fri, 2007-09-07 at 15:26 +0200, Norbert Hartl wrote:
> > Hi,
> > 
> > is the order of elements in a dictionary preserved?
> > I mean if I can assume that the first element added
> > stays the first element in associations, keys and
> > values?
> > 
> Oh, I tested it the wrong way. I can easily that the
> answer to my question is no :) So I need to use two
> orderedcollections or is there something appropriate?

It really depends on what you are actually trying to do. You can
use an OrderedCollection of associations to have ordered key,
value pairs:
OrderedCollection new
    with: #key1 -> #value1;
    with: #key2 -> #value2.

(note that a Dictionary is just a Set of associations). Or there
may be a more appropriate data structure for your problem, like
Categorizer, which provides a grouping for all elements in an
OrderedCollection, and is how Class categories and method
protocols are stored.

Or, if all else fails, it is really easy to make a new data
structure. Subclass Association to provide a 'next' field, and
subclass Dictionary to use these new LinkedAssociations, and now
you have a linked list overlaid on top of a dictionary

-- 
Matthew Fulmer -- http://mtfulmer.wordpress.com/
Help improve Squeak Documentation: http://wiki.squeak.org/squeak/808


More information about the Beginners mailing list