Question: Q 6 Your turn to Hash! 5 Points Grading comment: Assume you are hashing a set ( unknown length ) of randomly generated Strings, into

Q6 Your turn to Hash!
5 Points
Grading comment:
Assume you are hashing a set (unknown length) of randomly generated Strings, into a HashTable with a a size of 50(this means your HashTable has 50 buckets). You can also assume that Strings are iterable and Characters utilize the Java native hash function.
Examine the two hashCode() implementations below. In general, which hashCode() option will result in a greater number of collisions for all Strings? Justify your answer in 1-2 sentences describing how each hashCode may generate a range of possible hashCode values.
Assume that when we call this in our hash functions, we are referring to each String object we are hashing.
Option 1:
This code utilizes Java's implementation of hashCode() for Characters which returns the unique int value associated with each character based on its assigned ASCII value. Ex: 'a' returns 97,'A' returns 65.
public int hashCode1(){
Iterator iterator = this.iterator();
int result =0;
int i =0;
while (iterator.hasNext()){
result += iterator.next().hashCode();
i++;
}
return result;
}
Option 2:
This code utilizes Java's implementation of hashCode() for Strings which is the following (value is an array of the characters within the array):
//Java's String hashCode implementation
public static int hashCode(byte[] value){
int h =0;
int length = value.length;
for (int i =0; i < length; i++){
h =31* h + getChar(value, i);
}
return h;
}
public int hashCode2(){
return this.hashCode().
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!