|
The equals method in the Measurement class was originally defined something like this:
public boolean equals (Measurement m) { return m.myFeet == this.myFeet && m.myInches == this.myInches; }
With Measurement.equals defined in that way, but with a correctly defined hashCode method, what is the contents of the hash table after execution of the following program segment?
HashMap table = new HashMap ( ); table.put (new Measurement (6, 2), "clancy"); // Mike Clancy is 6'2" table.put (new Measurement (6, 2), "hale"); // so is Paul Hale
Basically I'm unsure of whether or not this actually changes anything. I think there might be an error, because when it checks the bucket, it will check if the new measurement .equals an object, but if not then it should work fine right?
|
"it will check if the new measurement .equals an object, but if not then it should work fine right?"
not sure what you mean there. but assuming that the rest of the code is correct. then when collision happens (when 1 measurement have more than 1 object associated with it.), then code should handle that (which what a hashtable should do). So if you code doesn't do that then you will have problems but for the above question then yes it will work so long nothing equals.
|
actually, it depends on the definition of thje HashMap class. If it handles collision for you (which i'm assuming it will since this is java ~_~) then what is the point of the equals method when all you want is to put them on a hash table.
|
doesnt it have to be public boolean equals (Object o) in order to get called internally by hashmap implementation?
|
The content is only the String "hale" with the measurement with value 6,2 as key.
Edit: Well this is the solution, its not going to help you much I guess. I'll explain:
HashTable, which implements the map interface, maps the keys you put in to specific values. Keys are compared with the compareTo(Object o) method, inherited by Object. In this case it will use the compareTo(Measurement m) method, defined in Measurement (can't formulate a correct explanation for this with my English ). When adding the second object, the HashTable will find that key and replace the value with the new one (also returning the old value).
|
the hash table should have both clancy and hale mapped to measurement (6, 2), but you wouldn't be able to access either of those. this is because since equals is defined incorrectly (needs to have object as a parameter), the second put() wouldn't overwrite the first.
|
On April 11 2008 08:08 nobodyhome246 wrote: the hash table should have both clancy and hale mapped to measurement (6, 2), but you wouldn't be able to access either of those. this is because since equals is defined incorrectly (needs to have object as a parameter), the second put() wouldn't overwrite the first.
Uh, wrong.
Measurement is an Object, and in this case Java will use the more specific method, which is compareTo(Measurement m).
Even if thats not the case, the map won't store 2 values for 1 key.
|
thanks for all the responses :0
|
a decent hash implementation should always handle multiple keys...
in C and C++ templates, multiple objects per key is assumed. so if your program should have any sophosication then you just need to reimplement your lookup function or method or wateva so it can check across keys with multiple objects.
not saying that should be done tho ~_~
|
On April 11 2008 08:12 h3r1n6 wrote:Uh, wrong.
Measurement is an Object, and in this case Java will use the more specific method, which is compareTo(Measurement m).
Even if thats not the case, the map won't store 2 values for 1 key.
wrong.
HashMap works with Objects of Static Type Object since it cannot know the Runtime Type of the objects it handles and thus, only equals(Object o) is getting called (if the hashcode matches).
here is a working version: http://nopaste.info/15b02d1475.html
Output: {Measurement@bc=hale}
Also, HashMap will store 2 values for the "same" key if they have the same Hashcode, but do not return true in the equals Method. You can try this by changing line 18 in the above code to "return false;"
Output: {Measurement@bc=hale, Measurement@bc=clancy}
|
Because if equals returns false then its not the same key... HashMap still won't store 2 values for 1 key.
But I was wrong with using the other method. Makes sense, because the map stores it as an Object and doesnt (need to) know the type. Man, I should think before I write answers.
|
|
|
|