Java Reference
In-Depth Information
708
Table 1 Sample Strings and Their Hash Codes
String
Hash Code
ÐAdamÑ
2035631
ÐEveÑ
70068
ÐHarryÑ
69496448
ÐJimÑ
74478
ÐJoeÑ
74656
ÐJulietÑ
Ȑ2065036585
ÐKatherineÑ
2079199209
ÐSueÑ
83491
It is possible for two or more distinct objects to have the same hash code; this is
called a collision. A good hash function minimizes collisions. For example, the
String class defines a hash function for strings that does a good job of producing
different integer values for different strings. Table 1 shows some examples of strings
and their hash codes. You will see in Section 16.4 how these values are obtained.
A good hash function minimizes collisionsȌidentical hash codes for different
objects.
Section 16.4 explains how you should redefine the hashCode method for other
classes.
A hash code is used as an array index into a hash table. In the simplest
implementation of a hash table, you could make an array and insert each object at the
location of its hash code (see Figure 5 ).
 
Search WWH ::




Custom Search