Hash tables If we place n distinct elements into a hash table of size m using a
ID: 3576015 • Letter: H
Question
Hash tables If we place n distinct elements into a hash table of size m using a good hash function, how many elements do we expect to find in each table position? Recall the multiplicative hash function hash(x) = (x.hashCode() * z) Gg w-d. In 32-bit Java, what is the value of w? How large is the table that is used with this hash function? (In other words, what is the range of this hash function?) Write this function in more standard mathematical notation using the mod and div (integer division) operators. Explain the relationship between an class' hashCode() method and its equals (o) method. Explain, in words, what is wrong with the following hashCode() method: public class Point2D {Double x, y;. .. public int hashCode() {return x.hashCode() DownBreve y.hashCode();}} Give an example of many points that all have the same hashCode(). Explain, in words, what is wrong with the following hashCode() method: public class Point2D {Double x, y;. .. public int hashCode() {return x.hashCode() + y.hashCode();}} Give an example of 2 points that have the same hashCode().Explanation / Answer
1) The no. Of elements in each hash table position is called Load Factor = n/m
3) hashcode() function is used to find in which table position the key or element should be placed.
equals() method is overriden to fefine equality between two object.
If you are overriding equals method, you should override hashcode method.
4) While writing user-defined hashcode() function, you should use @Override keyword.
Example : point pairs (1.0,2.0) and (2.0,1.0), (4.4,3.7) and (3.7,4.4) have same hashcode.
5) While writing user-defined hashcode() function, you should use @Override keyword.
Example : point pairs (1.5,2.5) and (2.5,1.5) have same hashcode.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.