The java program hashCodeString.java in the collection of Course Files , illustr
ID: 3579579 • Letter: T
Question
The java program hashCodeString.java in the collection of Course Files, illustrates the Java hashCode() method applied to strings. It shows how a small change in a string object can produce a significant change in the object’s hash code.
Since Java’s hashCode() method produces (only) a 32-bit integer, it should be possible to find two different words that have the same hash code. Try to do it this way:
Create a map in which the Key Set consists of Integers, and the Value Set consists of sets of String objects. You can use
Map<Integer, HashSet<String>> or
Map<Integer, TreeSet<String>>
Populate the map by reading a very large file of words. When you read a word, compute its hash code, h, and then add the word to the set whose key is h. Finally, iterate through all the keys and print the sets whose size is greater than one.
Of course you should test your program on a small file of words before you try it on a very large file.
This is actually a very easy program to write. The problem is finding a very large file of words and having the time and space resources to run your program.
Hand-in our source code, a link to the file of words that you used, and tell us which words you found that have the same hash code. (Please don’t try to submit the file of words! Just tell us where to find it)
Explanation / Answer
Here the Program that Contains Key as HashCode and value as HashSet<String>
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Courses {
public static void main(String args[]) {
try{
HashMap<Integer, HashSet<String>> map = new HashMap<Integer, HashSet<String>>();
FileReader fr = new FileReader("D:\Sample.txt"); // Apply for this data as sample
BufferedReader br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null) {
String arr[] = line.split(" ");
int i=0;
while(arr.length > i){
HashSet<String> hs=null;
if(map.containsKey(arr[i].hashCode())){ // Checks whether map contains same hashCode
hs = map.get(arr[i].hashCode()); // if so add them to set
hs.add(arr[i]);
map.put(arr[i].hashCode(), hs); // and keep in same hashCode Key
} else {
hs = new HashSet<String>(); // if not add to HashSet and add to Map
hs.add(arr[i]);
map.put(arr[i].hashCode(), hs);
}
i++;
}
}
// get map using entrySet that returns Entry of Keys and Values
// Keys are HashCodes and Values are HashSet
Set<Entry<Integer, HashSet<String>>> entrySet = map.entrySet();
Iterator<Entry<Integer, HashSet<String>>> itr= entrySet.iterator();
while(itr.hasNext()) {
Entry<Integer, HashSet<String>> entry = itr.next();
HashSet<String> value = entry.getValue();
if(value.size() > 1) { // value is HashSet and if size > 1 then printing that
System.out.println(entry.getKey() +" " + value);
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.