Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Cocoa/GNUstep (Objective-C): Simplified Objective-C example and added alternative using NSFastEnumeration.
m Disambiguated link (immutable)
Line 384:
In order to maintain this contract, a class that overrides <code>equals()</code> must also override <code>hashCode()</code>, and maybe vice versa, so that <code>hashCode()</code> is based on the same properties (or a subset of the properties) as <code>equals()</code>.
 
A further contract that hashed data structures has with the object is that the results of the <code>hashCode()</code> and <code>equals()</code> methods will not change once the object has been inserted into the map. For this reason, it is generally a good practice to base the hash function on [[Immutable object|immutable]] properties of the object.
 
Analagously, TreeMap, and other sorted data structures, requires that an ordering be defined on the data type. Either the data type must already have defined its own ordering, by implementing the {{Javadoc:SE|java/lang|Comparable}} interface; or a custom {{Javadoc:SE|java/util|Comparator}} must be provided at the time the map is constructed. Like with HashMap above, the relative ordering of keys in a TreeMap should not change once they have been inserted into the map.