Re: Hashtable ordering
Eric Sosman wrote:
Frank Cisco wrote:
What determines the ordering of an unsorted hashtable?
Answer #1: The hash table's source code, along with the code
for the objects used as keys. Java has more than one kind of hash
table (HashMap and Hashtable are two, and there are others for more
specialized purposes), and they are likely to do things somewhat
differently. Study the source for the class (or classes) that you're
interested in.
Always remembering that anything you learn by studying the source code,
rather than the API documentation, may change from release to release.
Answer #2: The phase of the moon and the whim of the Gods.
If you get a certain ordering with today's version of the table,
it does not follow that tomorrow's version won't change it. Even
tomorrow's execution of the same program with the same table class
might change it. In short, if you need a particular order you must
take explicit steps to get it; you can't rely on an unsorted hash
table to order things reproducibly.
Even within one instance of the hash table, it can change without
notice. For example, consider a hash table such as HashMap that
automatically increases the number of buckets and rebuilds itself when
the load factor exceeds a limit. Two keys that were in the same bucket,
and so ordered by when they were inserted, may now be in different
buckets, and ordered according to some function of their hash codes.
It is not just if you need a particular order. If you even need a
consistent order, you can't rely on an unsorted hash table.
Patricia