HashMap
I have just been reading (JDK1.3: ch10) about Sets and Vectors and Hash maps. I was wondering about the HashMap entries: a pair of an object and a key. The key object's hashCode refers to the value object. That enables you to find it later in a filed HashMap, like in the Phone book example: where the String name is the key to finding the phone number.
Now isn't it simpler to just add a "phone number" data member to the Person class, and put all those "persons" in a Person[] array (or perhaps in a Vector)?
What then are the advantages of using a HashMap?
And why all this trouble of creating keys in a way that is PRONE TO ERROR? As two different objects could generate a hashCode that is the same. Isn't that a bit clumsy?
So, what is the practical use of a HashMap?
Please..
Francis
The Netherlands
|