The java program hashCodeString.java in the collection of Course Files , illustr
ID: 3581794 • 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
Note : Please use any other large word text file in the same format given in the program
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class HashCodeString {
public static void main(String[] args) {
Map<Integer, HashSet<String>>wordMap=new HashMap<>(); //map to store words
readInputFile(wordMap);
//printing words with their hashcode
for (Map.Entry<Integer, HashSet<String>>entry : wordMap.entrySet()) {
if(entry.getValue().size()>0){
System.out.println("Hashcode "+entry.getKey()+" : "+entry.getValue()+"");
}
}
}
//read the file and insert the words inside map
public static void readInputFile(Map<Integer, HashSet<String>>wordMap) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("D:\words.txt"));// reading
// the
// file
int count = 0;
while ((sCurrentLine = br.readLine()) != null) {// loop through the
// end of the file
int h=sCurrentLine.hashCode();
if(wordMap.containsKey(h)){
wordMap.get(h).add(sCurrentLine);
}else{
HashSet<String>set=new HashSet<>();
set.add(sCurrentLine);
wordMap.put(h, set);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
----------------------------------------------------------------output----------------------------------------------------------------------------
Hashcode 96673 : [all]
Hashcode -1396355227 : [banana]
Hashcode 93621297 : [below]
Hashcode 1660813752 : [sorround]
Hashcode 3559070 : [this]
Hashcode 3173020 : [girl]
Hashcode 97740 : [boy]
Hashcode 103662530 : [mango]
Hashcode -1008851410 : [orange]
Hashcode 3558823 : [that]
Hashcode 93029210 : [apple]
Hashcode 96727 : [and]
Hashcode 107990 : [men]
Hashcode 92611485 : [above]
Hashcode 113313790 : [women]
-----------------------------------------------------------------------------words.txt--------------------------------------------------------------
and
this
that
and
all
above
below
sorround
this
girl
boy
men
women
banana
apple
mango
orange
apple
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.