The best hash functions have the most amount of clustering. True/False For a cla
ID: 3816520 • Letter: T
Question
The best hash functions have the most amount of clustering. True/False For a class with an equals function, function hashCode has to satisfy a certain property. What is that property? Below is part of class HashMe. Circle the best hash function for it from the list below. Also, underline any valid hash functions (they could be terrible, but as long as they work). Assume that time0fDaylnSeconds() returns an int. Justify your rationale (a) return 0; (b) return id; (c) return x; (d) return timeOfDaylnSeconds(); public class HashMe() {private final int id;//Won't change after construction, used in equals(). private int x;//Might change after construction, not used in equals(). @ Override public int hashCode() {//What goes here?}//... other methods...}Explanation / Answer
1) Clustering during hashing means when two or more elements try to accomodate in one slot of hash table. Best Hash function will always avoid this kind of activity. So Best Hash function have the least amount of clustering . So its False
2) If a class is having equals method overridden, it needs to have hashcode method also overridder. The use of hashcode function is that suppose application is in execution, then the hashcode returned for the object under execution must be same till the next execution of application is done/ or the first execution is stopped. The property which hashcode needs to satify along with equals is that If for two objects a and b, a.equals(b) is true , than the integer returned by a.hashcode() and b.hashcode() should be same. So equals and hashcode follow same purpose but in different context.
3) As I told in the previous question, hashcode needs to satisfy the property that if two objects are equal, there hashcodes must return same integer throughout the whole execution of the application. so out of the given options: -
1. timeOfDayInSeconds() will not work here because it will change with time, So for two objects which are same it can return different values, while application is under execution.
2. return 0 is a very bad choice , as it will return same integer(0) for two objects which are not equal at all. We can use it but it will be just against the whole purpose of using hash table.
3.return x is not used in equals and might change while in execution, but there are chances that it will remain same for two objects which are same. Chances are very steep but there are chances. Because of modification possible also, this becomes a bad choice.
4. The best choice will be return id. As in equals we are comparing two object with ids and if ids are equal both objects will be equal , So there hashcode must return same integer that is id. So return id will be the best hash function here.So correct answer is b).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.