[Java] Please test your code in the link I provide before you post your answer.
ID: 3835657 • Letter: #
Question
[Java] Please test your code in the link I provide before you post your answer.
The output should be looked like exact same as the tester.
http://www.codecheck.it/files/17040703336th1j7tfhqsu83tf1bute4l7u
Thank you.
Write a program HashCodeSearch to search a file to see if you can find two words that have the same hash code. Read the file with a Scanner and use a data structure that maps the hashcode to a set of words with that hashcode. At the end, print out words that have the same hash code on the same line in lexicographical order. (That will be any set with a length greater than 1)
Finally print the number of duplicates divided by the number of words. It will be a really small number. That is the percentage of collisions.
Set the delimiter to use: scan.useDelimiter("[^A-Za-z0-9_]+");
The book to search is at this URL(http://www.gutenberg.org/ebooks/2600) . Download the plain text and save it in your project as "war_and_peace.txt". The book is "War and Peace" - so there will be lots of words. (586,914).
You can just declare that the main method throws FileNotFoundException. No need for a try/catch
Explanation / Answer
In the link provided above to check the code, I am not able to run it as it is saying classnotfoundexception and I have named the class same only.
It was not given whether to consider duplicates or not, So I am using TreeSet which dont allows duplicates.I have used delimiter provided by you and text file. But it is not actually reading whole file but only a part of file.But as per the instructions, I have run with your delimiter only.
Here is program below: -
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
public class HashCodeSearch {
public static void main(String[] args) throws FileNotFoundException {
// Creating wordCountMap which holds words as keys and their occurrences
// as values
HashMap<Integer, TreeSet<String>> wordCountMap = new HashMap<Integer, TreeSet<String>>();
File file;
file = new File("war_and_peace.txt");
// Reading the first line into currentLine
Scanner reader = new Scanner(file);
reader.useDelimiter("[^A-Za-z0-9_]+");
String currentLine = "";
TreeSet<String> tree;
while (reader.hasNext()) {
currentLine = reader.next();
if (wordCountMap.containsKey(currentLine.toLowerCase().hashCode())) {
tree = wordCountMap.get(currentLine.toLowerCase().hashCode());
tree.add(currentLine);
} else {
tree = new TreeSet<>();
tree.add(currentLine);
wordCountMap.put(currentLine.toLowerCase().hashCode(), tree);
}
}
reader.close();
int wordCount = 0;
int duplicate = 0;
for (HashMap.Entry<Integer, TreeSet<String>> entry : wordCountMap.entrySet()) {
tree = entry.getValue();
wordCount = wordCount + tree.size();
if (tree.size() > 1) {
duplicate = duplicate + tree.size();
System.out.println(tree);
}
}
System.out.println("Number of words:"+wordCount);
System.out.println("Number of duplicates:"+duplicate);
System.out.println("Collision Percentage:"+duplicate/(double)wordCount);
}
}
Sample Output: -
[PEACE, Peace]
[By, by]
[The, the]
[EBOOK, EBook, eBook]
[PROJECT, Project]
[THIS, This, this]
[AND, and]
[OF, of]
[WAR, War]
[GUTENBERG, Gutenberg, gutenberg]
Number of words:160
Number of duplicates:23
Collision Percentage:0.14375
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.