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

Content deleted Content added
added objective c
Line 310:
</source>
 
The <code>get<{{Javadoc:SE|java/code>util|Map|get(java.lang.Object)|name=get}} 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.
Line 322:
* <source lang="java">if a.equals(b), then a.hashCode() == b.hashCode()</source>
 
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]] properties of the object.
Line 528:
SET NAME=""
FOR S NAME=$ORDER(^phonebook(NAME)) QUIT:NAME="" WRITE NAME," Phone Number :",^phonebook(NAME),!
 
=== [[Objective-C]] ===
In the [[Cocoa (API)|Cocoa]] and [[GNUStep]] frameworks, associative arrays are implemented by the <code>NSDictionary</code> (immutable) and <code>NSMutableDictionary</code> (mutable) classes. They are implemented with hash tables.
 
<source lang="objc">
NSDictionary *phoneBook = [NSDictionary dictionaryWithObjectsAndKeys:
@"555-9999", @"Sally Smart",
@"555-1212", @"John Doe",
@"555-1337", @"J. Random Hacker",
nil];
</source>
 
The <code>objectForKey:</code> method is used to access a key; for example, the value of the expression <code>[phoneBook objectForKey:@"Sally Smart"]</code> is <code>@"555-9999"</code>.
 
The hash function in Objective-C is provided by the method <code>hash</code> in NSObject. Since every class in Cocoa/GNUStep [[Inheritance (computer science)|inherits]] from <code>NSObject</code>, every object has a hash function. A class can [[Method overriding (programming)|override]] the default implementation of <code>hash</code> to provide a custom hash function based on the properties of the object.
 
The <code>NSObject</code> class also contains the method <code>isEqual:</code> that tests the object for equality with another object. Dictionaries in OCaml rely on objects maintaining the following contract between their <code>hash</code> and <code>isEqual:</code> methods:
 
For two objects ''a'' and ''b'',
* <source lang="objc">[a isEqual:b] == [b isEqual:a]</source>
* <source lang="objc">if [a isEqual:b], then [a hash] == [b hash]</source>
 
In order to maintain this contract, a class that overrides <code>isEqual:</code> must also override <code>hash</code>, and maybe vice versa, so that <code>hash</code> is based on the same properties (or a subset of the properties) as <code>isEqual:</code>.
 
A further contract that the dictionary has with the object is that the results of the <code>hash</code> and <code>isEqual:</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]] properties of the object.
 
=== OCaml ===