Content deleted Content added
Line 312:
The <code>get</code> method is used to access a key; for example, the value of the expression <code>phoneBook.get("Sally Smart")</code> is <code>"555-9999"</code>.
This code above uses a hash map to store the associative array, by calling the constructor of the {{Javadoc:SE|java/util|HashMap}} class; however, since the code only uses methods common to the interface {{Javadoc:SE|java/util|Map}}, one could also use a self-balancing binary tree by calling the constructor of the {{Javadoc:SE|java/util|TreeMap}} class (which implements the subinterface {{Javadoc:SE|java/util|SortedMap}}), without changing the definition of the <code>phoneBook</code> variable or the rest of the code, or use a number of other underlying data structures that implement the <code>Map</code> interface.
The hash function in Java, used by HashMap and HashSet, is provided by the method {{Javadoc:SE|java/lang|Object|hashCode()}}. Since every class in Java [[Inheritance (computer science)|inherits]] from {{Javadoc:SE|java/lang|Object}}, every object has a hash function. A class can [[Method overriding (programming)|override]] the default implementation of <code>hashCode()</code> to provide a custom hash function based on the properties of the object.
The <code>Object</code> class also contains the method {{Javadoc:SE|name=equals(Object)|java/lang|Object|equals(java.lang.Object)}} that tests the object for equality with another object.
For two objects ''a'' and ''b'',
Line 324:
In order to maintain this contract, a class that overrides <code>equals()</code> must also override <code>hashCode()</code>, and 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
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.
=== JavaScript ===
|