comparing Collections. Can we do better!?

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Dec 16 03:46:14 UTC 2003


Joel Shellman <joel at ikestrel.com> wrote:
	For another example of hash problems:
	
	It used to be that in the standard String class in Java, the hashCode 
	was calculated every time it was called. This led to people recommending 
	never to use String's as keys in Hashtables, but rather create their own 
	Key class.
	
	Fortunately, they did change String's implementation so it now caches 
	the hash code.
	
Since the only immutable Collection class in Squeak that I can call to mind
is Interval, it's not clear to me that this example tells us anything useful.
(Except that having some immutable collections might be a good idea.)

Java has unmodifiable collections.  More precisely, if you ask for an
unmodifiable version of a collection, you get an unmodifiable *wrapper*
around the same modifiable collection you started with.  I don't know of
any Java collections (Strings aren't Collections in Java) that cache their
hash code.

	That's another handy thing about immutable classes. You can cache the 
	hash code without worrying about it changing.
	
Java's unmodifiable collections don't do this; since they are just
wrappers around modifiable objects that might be changed by another route,
I don't believe that they _could_ do this.



More information about the Squeak-dev mailing list